Autolayout problems with iOS8 with code that works fine on iOS7 Autolayout problems with iOS8 with code that works fine on iOS7 ios ios

Autolayout problems with iOS8 with code that works fine on iOS7

@robmayoff has a great answer for this:

Essentially, in iOS8 you can no longer call setNeedsUpdateConstraints and setNeedsLayout on a view and expect the constraints of subviews to update.

You must call these methods on the view whose constraint is changing. This is backwards compatible to iOS7.


Suppose you have a ViewController with root view self.view and a subview called containerView. containerView has a NSLayoutConstraint attached to it that you want to change (in this case, top space).

In iOS7 you could update all constraints in a VC by requesting a new layout for the root view:

self.containerView_TopSpace.constant = 0;[self.view setNeedsUpdateConstraints];[self.view setNeedsLayout];

In iOS8 you need to request layouts on the containerView:

self.containerView_TopSpace.constant = 0;[self.containerView setNeedsUpdateConstraints];[self.containerView setNeedsLayout];

You might find the answers to this question helpful: UICollectionView cell subviews do not resize

In most cases the works in iOS7 but not on iOS 8 auto layout problems seem to stem from the root view not being sized correctly in iOS 8, particularly when we set translatesAutoresizingMaskIntoConstraints to NO. For my views I was able to set the root view's frame in layoutSubviews (or whichever appropriate initializer that does have the correct bounds) and this resolved the issue.

self.contentView.frame = CGRectInset(self.bounds, 0, 0);

As shown in the answer above, you could also do

self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

and then turn translatesAutoresizingMaskIntoConstraints back to NO before you start setting your own constraints in code.

Definitely hate that so much of our time is taken with these annoying gotchas.

In my case, the problem related to constraints labeled UIView-Encapsulated-Layout-Width and UIView-Encapsulated-Layout-Height. When I removed them, everything behaved as though my view was of zero size, with everything centered on the upper left corner of the screen. When I left them in, new constraints worked as expected. I also retained the constraints labeled _UILayoutSupportConstraint.