且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

上iOS7工作正常,自动布局问题iOS8上与code

更新时间:2023-02-03 09:56:11

@robmayoff有一个很好的答案:http://***.com/a/26066992/1424669

@robmayoff has a great answer for this: http://***.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];