【问题标题】:Xamarin iOS Center TableViewCell SubviewXamarin iOS 中心 TableViewCell 子视图
【发布时间】:2018-09-17 00:47:41
【问题描述】:

我正在使用 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);

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

【问题讨论】:

    标签: ios xamarin xamarin.ios icarousel


    【解决方案1】:

    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 类中设置,而不是在父 ViewController 类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多