Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...