iOS开发者进阶

Masonry更新约束的实战技巧

2020-08-20  本文已影响0人  __Mr_Xie__

前言

Masonry 框架使用过程中,有这么一个场景:某个控件的约束已经添加了,后面需要改变这个控件的某个约束,这个时候就需要更新这个约束。

更新约束过程

  1. 声明属性
@property (strong, nonatomic) MASConstraint *topConstraint;
  1. 存储约束
[self.amountNoticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@(15));
        make.right.mas_equalTo(@(-15));
        // 存储约束
        self.topConstraint = make.top.mas_equalTo(384);
        make.bottom.mas_equalTo(@(-10));
    }];
  1. 移除约束
[self.topConstraint uninstall];

注:与移除约束相对的是添加约束:

[self.topConstraint install];
  1. 更新约束
[self.amountNoticeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        self.topConstraint = make.top.mas_equalTo(360);
}];

Author

如果你有什么建议,可以关注我的公众号:iOS开发者进阶,直接留言,留言必回。

上一篇 下一篇

猜你喜欢

热点阅读