Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout problems with iOS8 with code that works fine on iOS7

Tags:

I'm in the midst of developing an app for the iPhone and iPad. It supports iOS6 and iOS7 and it uses auto layout exclusively.

This past week, when Apple announced that iOS8 was ready for prime-time, I upgraded one of my iPhones and an iPad both to iOS8. I also bumped my XCODE up to version 6. I have 2nd iPhone which I left at iOS7.

I generated new executables with Xcode 6 and I was distressed to see that their screen layouts were messed up when executed on my devices running iOS8 but still fine on iOS7. This is true on both my physical devices and on Xcode's emulators.

It took a lot of digging but I'm pretty clear now on what's happening though I don't know why.

Specifically, certain auto layout operations are failing for me on iOS8 but they are fine on iOS7.

Some examples involving a button which I am placing on an underlying view whose size is equal to the size of the screen:

(1) If I ask auto layout to position the button's horizontal center (CX) equal to the underlying view's horizontal center, the result is that the button's horizontal center is placed on the underlying view's left edge.

(2) If I ask auto layout to to make the width of the button equal to 50% of the width of the underlying view, it gives it no width at all.

I am able to work around these issues as follows:

(1) I ask auto layout to position the button's center equal to the underlying view's left edge plus 50% of the screen's width.

(2) I ask auto layout to make the button's width equal to 50% of the screen's width.

I am slowly clawing my way, with workarounds like these, back to auto layout code that works for me on both iOS7 and iOS8. But I am really wondering what's going on here.

It looks like auto layout cannot determine the size of the underlying view and so auto layout calculations that require that information fail. But it does know where the top and left edges of the view are so calculations based on those data succeed.

This is a large app and I've written many hundreds of lines of auto layout code for iOS6 and iOS7 that work perfectly for me.

I've been tweaking and trying things now with iOS8 for three days and I'm no wiser than I was when I began.

Anyone have any suggestions or thoughts as to what might be the issue here?

like image 647
Gallymon Avatar asked Sep 23 '14 05:09

Gallymon


2 Answers

@robmayoff has a great answer for this: https://stackoverflow.com/a/26066992/1424669

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.

EXAMPLE:

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]; 
like image 172
BFar Avatar answered Nov 23 '22 23:11

BFar


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.

like image 42
Praneeth Wanigasekera Avatar answered Nov 24 '22 00:11

Praneeth Wanigasekera