且构网

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

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

更新时间:2023-02-03 10:30:50

@robmayoff 对此有很好的回答:https://***.com/a/26066992/1424669

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