title: Xamarin iOS Center TableViewCell Subview
body: <p>I´m using the NuGet-Package iCarousel as a Subview in UITableViewCell.
  Here is my code where I add the iCarousel object (this code is located in the ViewDidLoad method of my parent ViewController):</p>
  
  <pre><code>InvokeOnMainThread(async() =&gt; 
          {
              items = await ppCtrl.GetAllPerpetrationPartIds(masterProject.Id).ConfigureAwait(false); 
  
              if (items != null &amp;&amp; items.Any())
              {
                  InvokeOnMainThread(() =&gt;
                  {
                      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();
                  });
  
              }
          });
  </code></pre>
  
  <p>Now I want to vertically and horizontally center the iCarousel. So I added this code:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>My problem is now that the app Crashes at this line:</p>
  
  <pre><code>CarouselViewCell.ContentView.AddConstraints(centerConstraints);
  </code></pre>
  
  <p>I found in a similar post that you need to add the constraints to the assiciated UIView object. I tried several objects but with no success.</p>