<i id='qva3J'><tr id='qva3J'><dt id='qva3J'><q id='qva3J'><span id='qva3J'><b id='qva3J'><form id='qva3J'><ins id='qva3J'></ins><ul id='qva3J'></ul><sub id='qva3J'></sub></form><legend id='qva3J'></legend><bdo id='qva3J'><pre id='qva3J'><center id='qva3J'></center></pre></bdo></b><th id='qva3J'></th></span></q></dt></tr></i><div id='qva3J'><tfoot id='qva3J'></tfoot><dl id='qva3J'><fieldset id='qva3J'></fieldset></dl></div>
    <tfoot id='qva3J'></tfoot>

    <small id='qva3J'></small><noframes id='qva3J'>

    • <bdo id='qva3J'></bdo><ul id='qva3J'></ul>

    <legend id='qva3J'><style id='qva3J'><dir id='qva3J'><q id='qva3J'></q></dir></style></legend>

      1. iOS8 的自动布局问题,代码在 iOS7 上运行良好

        时间:2023-09-11
          <tbody id='t1Gyt'></tbody>

          <legend id='t1Gyt'><style id='t1Gyt'><dir id='t1Gyt'><q id='t1Gyt'></q></dir></style></legend>

              <i id='t1Gyt'><tr id='t1Gyt'><dt id='t1Gyt'><q id='t1Gyt'><span id='t1Gyt'><b id='t1Gyt'><form id='t1Gyt'><ins id='t1Gyt'></ins><ul id='t1Gyt'></ul><sub id='t1Gyt'></sub></form><legend id='t1Gyt'></legend><bdo id='t1Gyt'><pre id='t1Gyt'><center id='t1Gyt'></center></pre></bdo></b><th id='t1Gyt'></th></span></q></dt></tr></i><div id='t1Gyt'><tfoot id='t1Gyt'></tfoot><dl id='t1Gyt'><fieldset id='t1Gyt'></fieldset></dl></div>
            1. <small id='t1Gyt'></small><noframes id='t1Gyt'>

              • <tfoot id='t1Gyt'></tfoot>
                • <bdo id='t1Gyt'></bdo><ul id='t1Gyt'></ul>
                  本文介绍了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 中,您不能再在视图上调用 setNeedsUpdateConstraintssetNeedsLayout 并期望子视图的约束会更新.

                  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 上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:不同设备的单一大小类中的 iOS 不同字体大小 下一篇:在使用自动布局下动画/移动视图

                  相关文章

                  1. <i id='j84EJ'><tr id='j84EJ'><dt id='j84EJ'><q id='j84EJ'><span id='j84EJ'><b id='j84EJ'><form id='j84EJ'><ins id='j84EJ'></ins><ul id='j84EJ'></ul><sub id='j84EJ'></sub></form><legend id='j84EJ'></legend><bdo id='j84EJ'><pre id='j84EJ'><center id='j84EJ'></center></pre></bdo></b><th id='j84EJ'></th></span></q></dt></tr></i><div id='j84EJ'><tfoot id='j84EJ'></tfoot><dl id='j84EJ'><fieldset id='j84EJ'></fieldset></dl></div>

                    <small id='j84EJ'></small><noframes id='j84EJ'>

                        <bdo id='j84EJ'></bdo><ul id='j84EJ'></ul>
                      <tfoot id='j84EJ'></tfoot>
                      <legend id='j84EJ'><style id='j84EJ'><dir id='j84EJ'><q id='j84EJ'></q></dir></style></legend>