使用Masonry的mas_updateConstraints错误理解

本文深入探讨了Masonry布局库中的mas_updateConstraints方法,并通过具体示例解释了如何正确使用此方法来更新视图约束,避免布局冲突的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Masonry就不做过多的介绍了,搞iOS布局的应该都知道这个开源库,使用它能节省不少体力,最近在项目中使用这个库的mas_updateConstraints时,发现该方法和自己想象的有点不一样。先贴下自己的代码:

# BaseClass
 [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    self.textLabelLeftLayout = make.left.equalTo(self.checkedButton.mas_right);
    make.centerY.equalTo(self.mas_centerY);
    make.height.mas_equalTo(checkBoxWidth);
    make.right.lessThanOrEqualTo(self.mas_right);
}];

这是基类里对textlabel的布局,其相对view本身居中显示,而子类里想改变这种布局方式,改成和另外一个button对齐显示,因此我就在子类里调整布局如下:

# SubClass
  [self.textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.checkedButton.mas_centerY);
  }];

本以为这么做Masonry会帮我把centerY的布局更新,但是编译运行时会提示存在冲突,有两个centerY布局约束,不知道该使用哪一个,然后我就读了下Masonry的源码,发现原来mas_updateConstraints方法里对同一个布局的理解就是相对的元素也是一致才行,即这里这样做才算一次update:

# SubClass
  [self.textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.mas_centerY).offset(10);
  }];

所以,这里的一个update是针对如下这种情形才行:
A->B A->B的变化
A->C 这里是一个新的约束
源码里有个方法是对是否是同一个约束的判断:

- (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint {
    // check if any constraints are the same apart from the only mutable property constant

    // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints
    // and they are likely to be added first.
    for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) {
        if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue;
        if (existingConstraint.firstItem != layoutConstraint.firstItem) continue;
        if (existingConstraint.secondItem != layoutConstraint.secondItem) continue;
        if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue;
        if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue;
        if (existingConstraint.relation != layoutConstraint.relation) continue;
        if (existingConstraint.multiplier != layoutConstraint.multiplier) continue;
        if (existingConstraint.priority != layoutConstraint.priority) continue;

        return (id)existingConstraint;
    }
    return nil;
}

可以看到,需要firstItem,secondItem,firstAttribute,secondAttribute,relation,multiplier,priority等一致才会当做同一个约束,否则都不算做一个约束。所以在使用mas_updateConstraints时大家一定要分清楚是否update还是重新添加了一个约束。


PS:针对我遇到的这种使用情况,我在基类里将我的居中约束设置了一个优先级来处理的

# BaseClass
 [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    self.textLabelLeftLayout = make.left.equalTo(self.checkedButton.mas_right);
    make.centerY.equalTo(self.mas_centerY).priorityMedium();
    make.height.mas_equalTo(checkBoxWidth);
    make.right.lessThanOrEqualTo(self.mas_right);
}];
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值