Autolayout problems with iOS8 with code that works fine on iOS7(iOS8 的自动布局问题,代码在 iOS7 上运行良好)
问题描述
我正在为 iPhone 和 iPad 开发应用程序.它支持iOS6和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.
上周,当 Apple 宣布 iOS8 已准备好迎接黄金时段时,我将我的一部 iPhone 和一部 iPad 都升级到了 iOS8.我还将我的 XCODE 升级到了第 6 版.我有第二部 iPhone,我留在 iOS7 时.
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.
我使用 Xcode 6 生成了新的可执行文件,当我在运行 iOS8 但在 iOS7 上仍然正常的设备上执行时,我很痛苦地看到它们的屏幕布局被搞砸了.在我的物理设备和 Xcode 的模拟器上都是如此.
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.
具体来说,某些自动布局操作在 iOS8 上对我来说是失败的,但在 iOS7 上却很好.
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) 如果我要求自动布局将按钮的水平中心 (CX) 定位为等于底层视图的水平中心,结果是按钮的水平中心位于底层视图的左边缘.
(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) 如果我要求自动布局使按钮的宽度等于底层视图宽度的 50%,它根本没有宽度.
(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) 我要求自动布局将按钮的中心定位为等于底层视图的左边缘加上屏幕宽度的 50%.
(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) 我要求自动布局使按钮的宽度等于屏幕宽度的 50%.
(2) I ask auto layout to make the button's width equal to 50% of the screen's width.
我正在慢慢摸索自己的方式,使用这些变通方法,回到适用于 iOS7 和 iOS8 的自动布局代码.但我真的很想知道这里发生了什么.
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.
这是一个大型应用程序,我为 iOS6 和 iOS7 编写了数百行自动布局代码,非常适合我.
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.
我已经用 iOS8 进行了三天的调整和尝试,但我并不比刚开始时聪明.
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?
推荐答案
@robmayoff 对此有很好的回答:https://stackoverflow.com/a/26066992/1424669
@robmayoff has a great answer for this: https://stackoverflow.com/a/26066992/1424669
本质上,在 iOS8 中,您不能再在视图上调用 setNeedsUpdateConstraints
和 setNeedsLayout
并期望子视图的约束会更新.
Essentially, in iOS8 you can no longer call setNeedsUpdateConstraints
and setNeedsLayout
on a view and expect the constraints of subviews to update.
您必须在约束发生变化的视图上调用这些方法.这向后兼容 iOS7.
You must call these methods on the view whose constraint is changing. This is backwards compatible to iOS7.
示例:
假设您有一个带有根视图 self.view
的 ViewController 和一个名为 containerView 的子视图.containerView 有一个 NSLayoutConstraint
附加到它,你想改变它(在这种情况下,顶部空间).
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).
在 iOS7 中,您可以通过请求根视图的新布局来更新 VC 中的所有约束:
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];
在 iOS8 中,您需要在 containerView 上请求布局:
In iOS8 you need to request layouts on the containerView:
self.containerView_TopSpace.constant = 0;
[self.containerView setNeedsUpdateConstraints];
[self.containerView setNeedsLayout];
这篇关于iOS8 的自动布局问题,代码在 iOS7 上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS8 的自动布局问题,代码在 iOS7 上运行良好
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01