一个有趣的实验
首先做个简单的小实验,在storyboard拖放一个view,添加好约束。之后利用masonry分别去更新这个视图的位置,尺寸。会发现不一样的结果。
//更新尺寸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
[self.viewTest mas_updateConstraints:^(MASConstraintMaker *make) {
//和storyboard中的约束相同
make.width.mas_equalTo(375.0);
}];
}
//更新位置
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
[self.viewTest mas_updateConstraints:^(MASConstraintMaker *make) {
//和storyboard中的约束相同
make.left.equalTo(self.viewTest.superview);
}];
}
如果你这么做会惊奇的发现更新位置有效,更新尺寸无效。why?到底用masonry能不能更新xib上的约束?带着这个问题我们去深入探索。
探索
第一步:查看源代码
其实上面两个小实验控制台都有打印警告