使用 masonry mas_updateConstraints 的 时候 需要注意的事项

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);
}];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

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

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

所以,这里的一个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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

可以看到,需要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);
}];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2
时间: 2024-08-24 03:21:12

使用 masonry mas_updateConstraints 的 时候 需要注意的事项的相关文章

Masonry整理

Masonry整理 Masonry是以AutoLayout为基础的轻量级布局框架更加简化了整个约束系统 Masonry三方下载本文参考:    地址1    地址2    地址3    地址4 *Masonry有哪些属性 @property (nonatomic, strong, readonly) MASConstraint left;@property (nonatomic, strong, readonly) MASConstraint top;@property (nonatomic,

Masonry介绍与使用实践:快速上手Autolayout

以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 window的size固定为(320,480) 我们只需要简单计算一下相对位置就好了 在iphone4-iphone4s时代 苹果推出了retina屏 但是给了码农们非常大的福利:window的size不变 在iphone5-iphone5s时代 window的size变了(320,568) 这时autoresizingMask派上了用场(为啥这时候不用Autolayout? 因为还要支持ios5呗) 简单

iOS — Autolayout之Masonry解读

前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 window的size固定为(320,480) 我们只需要简单计算一下相对位置就好了 在iphone4-iphone4s时代 苹果推出了retina屏 但是给了码农们非常大的福利:window的size不变 在iphone5-iphone5s时代 window的size变了(320,568) 这时auto

使用第三方框架 Masonry 实现自动布局

使用第三方框架 Masonry 实现自动布局 时间:2015-02-10 11:08:41      阅读:4595      评论:0      收藏:0      [点我收藏+] 由于前两天都在学习自动布局的使用,但是又觉得苹果原生的方式太过于麻烦,而且也不易于理解,昨天听人说了有个第三方框架也可以实现自动布局的功能,然后在https://github.com/上找到了Mansonry这个框架,使用起来真的减少了很多时间,而且代码直观,更加容易理解. 送上源码地址:https://githu

AutoLayout框架Masonry使用心得

AutoLayout框架Masonry使用心得 AutoLayout的一些基本概念 利用约束来控制视图的大小和位置,系统会在运行时通过设置的约束计算得到frame再绘制屏幕 两个属性Content Compression Resistance(排挤,值越高越固定)和Content Hugging(拥抱),Masonry代码如下 //content hugging 为1000 [view setContentHuggingPriority:UILayoutPriorityRequired forA

masonry 使用笔记

Masonry的使用,动画,出现问题解决等 经过一点时间的使用,发现在网上很少有Masonry的教程,也仅仅有那么一两篇而已,在此我编写一下我最近一段时间使用的方法,供大家学习. Masonry是AutoLayout的一个第三方类库,用链式语法封装了冗长的AutoLayout代码,因此学习成本相对于官方提供的AutoLayout,以及VFL语言而言,低上很多很多... 准备 在GitHub上 https://github.com/SnapKit/Masonry 下载配置第三方库,基本使用方法在R

<转>iOS常用第三方库之Masonry

一.前言 关于苹果的布局一直是我比较纠结的问题,是写代码来控制布局,还是使用storyboard来控制布局呢?以前我个人开发的时候很少使用代码去写约束,因为太麻烦了.所以最终选择的都是AutoLayout进行布局,然后拖线设置约束.不过好多公司进行iOS开发的时候都会去动态的修改约束,而且有的会使用约束去创建一些动画,所以不太去用storyboard进行开发(还有就是使用storyboard几个人合作的时候比较麻烦).反倒更多的是写代码开发看起来更加的高效.所以好多开发者都开始去使用Masonr

【iOS】Masonry使用案例讲解

如果说自动布局解救了多屏幕适配,那众多三方库的出现就解救了系统自动布局的写法.Masonry就是其中一个. 在Github上,Masonry已经得到5000+个star,用法上也比较简单灵活,很大程度上替代了传统的NSLayoutConstraint布局方式.本文将利用几个案例来讲解Masonry的使用. Masonry下载地址: https://github.com/SnapKit/Masonry 本文Demo下载地址: ?https://github.com/saitjr/MasonryDe

iOS常用库之Masonry

简单介绍 Masonry 源码地址:https://github.com/Masonry/Masonry Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Mac OS X. ``` pod 'Masonry' ``` 使用Masonry需要导入头文件 `#import  "Masonry.h"`  系统API vs Masonry 系统API NSLayoutConstraint ```ob