Xamarin iOS中心TableViewCell子视图

时间:2018-04-06 23:12:03

标签: ios xamarin xamarin.ios icarousel

我在UITableViewCell中使用NuGet-Package iCarousel作为子视图。 这是我添加iCarousel对象的代码(此代码位于我的父ViewController的ViewDidLoad方法中):

InvokeOnMainThread(async() => 
        {
            items = await ppCtrl.GetAllPerpetrationPartIds(masterProject.Id).ConfigureAwait(false); 

            if (items != null && items.Any())
            {
                InvokeOnMainThread(() =>
                {
                    var carousel = new iCarousel
                    {
                        Bounds = CarouselViewCell.ContentView.Bounds,
                        ContentMode = UIViewContentMode.Center,
                        Type = iCarouselType.CoverFlow2,
                        Frame = CarouselViewCell.ContentView.Frame,
                        CenterItemWhenSelected = true,
                        DataSource = new SimpleDataSource(items, CarouselViewCell.ContentView.Bounds.Width, CarouselViewCell.ContentView.Bounds.Height),
                        Delegate = new SimpleDelegate(this)

                    };

                    NSLayoutConstraint centerX = carousel.CenterXAnchor.ConstraintEqualTo(this.CarouselViewCell.ContentView.CenterXAnchor);
                    NSLayoutConstraint centerY = carousel.CenterYAnchor.ConstraintEqualTo(this.CarouselViewCell.ContentView.CenterYAnchor);

                    centerX.SetIdentifier("centerXCostraint");
                    centerY.SetIdentifier("centerYConstraint");

                    var centerConstraints = new[] {centerX, centerY };

                    CarouselViewCell.ContentView.TranslatesAutoresizingMaskIntoConstraints = false;
                    CarouselViewCell.ContentView.AddConstraints(centerConstraints);

                    //NSLayoutConstraint.ActivateConstraints(centerConstraints);

                    CarouselViewCell.ContentView.ContentMode = UIViewContentMode.Center;
                    CarouselViewCell.ContentView.AddSubview(carousel);
                    ViewDidLayoutSubviews();
                });

            }
        });

现在我想垂直和水平地居中iCarousel。所以我添加了这段代码:

NSLayoutConstraint centerX = carousel.CenterXAnchor.ConstraintEqualTo(this.CarouselViewCell.ContentView.CenterXAnchor);
NSLayoutConstraint centerY = carousel.CenterYAnchor.ConstraintEqualTo(this.CarouselViewCell.ContentView.CenterYAnchor);

centerX.SetIdentifier("centerXCostraint");
                centerY.SetIdentifier("centerYConstraint");

var centerConstraints = new[] {centerX, centerY };
CarouselViewCell.ContentView.TranslatesAutoresizingMaskIntoConstraints = false;
                CarouselViewCell.ContentView.AddConstraints(centerConstraints);

我现在的问题是该应用程序在此行崩溃:

CarouselViewCell.ContentView.AddConstraints(centerConstraints);

我在一篇类似的帖子中发现,您需要将约束添加到关联的UIView对象中。我尝试了几个对象但没有成功。

1 个答案:

答案 0 :(得分:2)

  

CarouselViewCell.ContentView.AddConstraints(centerConstraints);

此代码将崩溃,因为我们需要先添加控件,然后设置其约束。或者会崩溃。尝试调整代码的顺序:

//Let this code just lie after the carousel's construction.
CarouselViewCell.ContentView.AddSubview(carousel);

此外,如果您想添加轮播的约束,则应将轮播的TranslatesAutoresizingMaskIntoConstraints设置为false而不是CarouselViewCell.ContentView

此外,根据您的描述,我认为您想将iCarousel放在UITableViewCell上。此配置应在Cell类中设置,而不是在父ViewController类中设置。