updateViewConstraints和updateConstraints

来源:互联网 发布:椅子 孩子写作业 知乎 编辑:程序博客网 时间:2024/05/27 16:42
  1. - (void)updateViewConstraints NS_AVAILABLE_IOS(6_0); 

UIViewController中也新增了一个更新布局约束的方法,在AutoLayout UIView相关API的笔记中,详细讲述了UIView的一组更新布局约束的方法。

这个方法默认的实现是调用对应View的 -updateConstraints 。ViewController的View在更新视图布局时,会先调用ViewController的updateViewConstraints 方法。我们可以通过重写这个方法去更新当前View的内部布局,而不用再继承这个View去重写-updateConstraints方法。我们在重写这个方法时,务必要调用 super 或者 调用当前View的 -updateConstraints 方法。



通过代码为xib或sb中view增加约束时,尽量避免在viewDidLoad中执行,最好放在updateViewConstraints[UIViewController]或者updateConstraints[UIView]中,记得调用[super updateViewConstraints]或者[super updateConstraints];

- (void)updateViewConstraints{    // 在这里为你的view添加约束,请确保该view的translatesAutoresizingMaskIntoConstraints属性已设置为NO    [super updateViewConstraints];}

如果你真的写在viewDidLoad里了,那么可能会遇到这种崩溃错误

Terminating app due to uncaught exception "NSInternalInconsistencyException"
0 0
原创粉丝点击