Pregunta

I'm finally discontinuing support for iOS 5 in my app, which means I can adapt my all my NIBs to use NSLayoutConstraint. It's been a real joy removing all the manual layout code that I've put in place over the years, but I've encountered a simple problem that seems that there should be an easy answer.

Here's the simplest way I can recreate it: I have a root view controller and a child view controller. The child loads it's view from a NIB. The view has a single child view that maintains 20 pt margin from each side, accomplished by 20 pt top, leading, trailing, and bottom.

The root view Controller has a container view that gets child's view gets put into and then moved around.

In RootViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *childView = self.childController.view;
    childView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.childContainerView addSubview: childView];
}

There are times that I completely collapse the child view. In RootViewController:

- (IBAction)collapseChild:(id)sender
{
    // Adjust the height constraint on the container view
    self.childHeightConstraint.constant = 0;
    [self.view setNeedsUpdateConstraints];
}

When I do this, I get an error:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

I expect it is because the topMargin (20) + bottomMargin (20) + innerViewHeight > totalHeight (0). How do I make it not complain about this when I collapse the view?

¿Fue útil?

Solución

I figured out the answer and thought it best to post it here so that it can aid others.

The problem was because all the constraints has required priority (UILayoutPriorityRequired in code or 1000 in Interface Builder). I reduced the priority in Interface Builder to 999 and the app now knows to break that constraint if it must.

enter image description here

Hopefully, this can help someone else in the future wade through a similar migration to mine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top