Xamarin iOS 居中 TableViewCell 子视图

Xamarin iOS Center TableViewCell Subview

我正在使用 NuGet-Package iCarousel 作为 UITableViewCell 中的子视图。 这是我添加 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);

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

CarouselViewCell.ContentView.AddConstraints(centerConstraints);

这段代码会崩溃,因为我们需要先添加控件,然后再设置其约束。否则它会崩溃。尝试调整代码顺序:

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

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

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

中设置