上iOS7工作正常,自动布局问题iOS8上与code [英] Autolayout problems with iOS8 with code that works fine on iOS7

查看:128
本文介绍了上iOS7工作正常,自动布局问题iOS8上与code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发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.

这过去的一周,当苹果宣布iOS8上准备好黄金时间,我升级我的iPhone手机之一,一个iPad既iOS8上。我还碰到我的X code到版本6。我有我留在iOS7第二个iPhone。

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.

我产生与X code 6新的可执行文件,我是心疼地看到,当上运行iOS8上,但仍然罚款iOS7我的设备执行他们的屏幕布局被搞砸了。这是我的两个物理设备和X上code的模拟器真实的。

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.

我花了很多挖的,但我pretty上发生的事情现在已经很清楚,虽然我不知道为什么。

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.

我慢慢地扣我的方式,像这些变通办法,回自动布局code,对我的作品上都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完美的我的工作,自动布局code线的。

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有一个很好的答案:<一href=\"http://stackoverflow.com/a/26066992/1424669\">http://stackoverflow.com/a/26066992/1424669

@robmayoff has a great answer for this: http://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 和一个名为containerView子视图一个的ViewController。 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];

这篇关于上iOS7工作正常,自动布局问题iOS8上与code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆