给Autolayout披上一层漂亮的外衣之后,将其称为Masonry,但Masonry的本质还是Autolayout。可以理解为Masonry是对Autolayout的封装.

对于一个约束。他实际表示的是一个不等或者相等关系

用Masonry创建一个完整的约束应该是这样的
//view1的左边距离父View左边10个点:
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(view1.superview.mas_left).multipliedBy(1).offset(10);
}];

Attribute

MASConstraintMaker

上面的表达式中,我们可以看到,make是MASConstraintMaker类型。MASConstraintMaker给我们提供了22种Attribute类型

//Basic Attribute
@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;//Margin Attribute
@property (nonatomic, strong, readonly) MASConstraint *leftMargin;
@property (nonatomic, strong, readonly) MASConstraint *rightMargin;
@property (nonatomic, strong, readonly) MASConstraint *topMargin;
@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;//Convenient Attribute
@property (nonatomic, strong, readonly) MASConstraint *edges;
@property (nonatomic, strong, readonly) MASConstraint *size;
@property (nonatomic, strong, readonly) MASConstraint *center;

Attribute总体来说分为三大类

  1. Basic Attribute: 基本属性,支持到iOS6,一般使用得比较多
  2. Margin Attribute: 边缘相关属性,支持到iOS8。由于版本要求比较高,一般用得比较少。
  3. Convenient Attribute: 便捷属性,为了使用方便而特意新增的属性。Autolayout本身没有对应的相关属性

Convenient Attribute实际是基本属性的组合。比如:edges表示left, right, top, bottom。
下面的两个代码实际的意义是一样的

//Convenient Attribute
make.edges.insets(edge);//Basic Attribute
make.left.right.top.bottom.insets(edge);

MASConstraint

前面我们看到MASConstraintMaker中所有的Attribute都是MASConstraint类型。对于多个Attribute一起写的表达式:

make.left.right.top.bottom.insets(edge);

make.left返回的已经是MASConstraint类型,也就是说right这个Attribute是MASConstraint的属性。
MASConstraint给我们提供了19种Attribute:

//Basic Attribute
@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;//Margin Attribute
@property (nonatomic, strong, readonly) MASConstraint *leftMargin;
@property (nonatomic, strong, readonly) MASConstraint *rightMargin;
@property (nonatomic, strong, readonly) MASConstraint *topMargin;
@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;

细看一下,MASConstraint中的Attribute和MASConstraintMaker完全一样。只是MASConstraintMaker中多了3种Convenient Attribute。

两者Attribute的一致,大大的提升了使用的方便性。使用过程中我们不用再去区分当前属性是MASConstraint还是MASConstraintMaker类型。(事实上没研究他的类型之前,我都不知道他们分别属于2种不同类的属性)

UIView

我们可以看到在.equalTo(view1.superview.mas_left)里面,superView也有Attribute。我们来看看UIView中有哪些Attribute:

// Basic Attribute
@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;// Margin Attribute
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins;

可以看出,在UIView中的Attribute和MASConstraint中的几乎一模一样,只是每一个Attribute加了一个mas_前缀。

由于UIView是系统的类,对其扩展属性和方法一般都需要添加自己的前缀,避免跟原有属性和方法冲突。不过他们的意义跟MASConstraint中的Attribute是相同的

Relationship

约束表示的是2个item之间的关系,在Autolayout中一共定义了3种关系:=, >=, <=,对应到Masonry中:

- (MASConstraint * (^)(id attr))equalTo;
- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))lessThanOrEqualTo;

相等关系我们一般用的多。那么不相等关系我们什么时候用呢?

假如我有一个Label。Label的长度不能超出父View,如果label中的文字比较短,我希望是文字有多长,Label就有多长。
由于label具有IntrinsicContentSize属性。所以默认情况下,他是文字有多长,Label就有多长。所以我们只需要设置Label的长度小于父View即可

[label mas_makeConstraints:^(MASConstraintMaker *make) {    make.left.offset(0);    make.centerY.offset(0);    make.width.lessThanOrEqualTo(label.superview);}];

multiplier

multiplier表示Attribute前面的乘数。Masonry提供了2种添加multiplier的方法

//    Sets the NSLayoutConstraint multiplier property
- (MASConstraint * (^)(CGFloat multiplier))multipliedBy;//    Sets the NSLayoutConstraint multiplier to 1.0/dividedBy
- (MASConstraint * (^)(CGFloat divider))dividedBy;

multipliedBy: 直接设置乘数
dividedBy: 设置乘数的倒数 multiplier = 1.0/dividedBy
一般宽或者高的约束使用multiplier比较多


Constant

Masonry提供了4种设置constant的方法

//Modifies the NSLayoutConstraint constant,only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
- (MASConstraint * (^)(MASEdgeInsets insets))insets;//Modifies the NSLayoutConstraint constant,only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following NSLayoutAttributeWidth, NSLayoutAttributeHeight
- (MASConstraint * (^)(CGSize offset))sizeOffset;//Modifies the NSLayoutConstraint constant, only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
- (MASConstraint * (^)(CGPoint offset))centerOffset;//Modifies the NSLayoutConstraint constant
- (MASConstraint * (^)(CGFloat offset))offset;
insets: 用来设置left, right, top, bottom。接受MASEdgeInsets类型值
sizeOffset: 用来设置width, height。接受CGSize类型的值
centerOffset: 用来设置centerX, centerY。接受CGPoint类型的值
offset: 可以用来设置所有的东西。接受CGFloat类型的值

小技巧

  1. 如果等式2边的Attribute是一样的,我们可以省略等式右边的Attribute
  2. 如果是等于关系,并且右边的view是父View。连equalTo也可以省略
  3. 如果equalTo里面传的是NSValue类型,效果跟设置offset是一样的
  4. 如果offset为0,其实也是可以省略的...

下面所有代码实际效果是一样的:

// 完整的
make.left.equalTo(view1.superview.mas_left).offset(0);//省略Attribute的
make.left.equalTo(view1.superview).offset(0);//省略equalTo的
make.left.offset(0);//使用equalTo替代offset的
make.left.equalTo(@0);//终极大招,省略所有的... 可惜会有warning
make.left;

对于这个警告我们可以将返回值转为空消除:

(void)make.left;

设置或更新约束

对于约束的设置,Masonry提供了3种方法,分别为设置约束、更新约束、重写设置约束

// 设置约束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;// 更新约束
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;// 重新设置约束
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

mas_makeConstraints: 初次设置约束使用。
mas_updateConstraints: 更新约束时使用。如果找不着这条约束,会新增,相当于mas_makeConstraints。
mas_remakeConstraints: 重新设置约束。先将view上所有约束移除,再新增约束

注意:mas_updateConstraints只能更新已有约束。如果第一次使用的是left, right设置的相对宽度。更新的时候想换成使用width。不能使用mas_updateConstraints,因为已有约束里面没有width的约束,新增width之后会跟原有left, right约束冲突。此时应该使用mas_remakeConstraints

批量设置约束

假设有View1,view2,view3三个View,我们想要他们的宽高都等于CGSizeMake(100, 50)。我们可以对他们进行批量设置:

NSValue *sizeValue = [NSValue valueWithCGSize:CGSizeMake(100, 50)];
[@[view1,view2,view3] mas_makeConstraints:^(MASConstraintMaker *make) {make.size.equalTo(sizeValue);
}];

由于我们还要设置view的top,left等位置约束。那可不可以在设置位置的mas_makeConstraints里面批量设置宽高呢?实际是可以的!

//advance set
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {(void)make.top.left;make.size.equalTo(@[view2,view3,sizeValue]);
}];

不过需要注意的是。设置约束的时候,view一定是已经被addSubview的,否则会抛异常。所以我们一般在最后一个view上加批量约束

Priority

我们知道约束是有优先级的,Masonry给我们提供了4个设置优先级的接口:

 //    Sets the NSLayoutConstraint priority to a float or MASLayoutPriority
- (MASConstraint * (^)(MASLayoutPriority priority))priority;//    Sets the NSLayoutConstraint priority to MASLayoutPriorityLow
- (MASConstraint * (^)())priorityLow;//    Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium
- (MASConstraint * (^)())priorityMedium;//    Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh
- (MASConstraint * (^)())priorityHigh;

priority: 可以设置任意的优先级,接受的参数是0-1000的数字
priorityLow: 设置低优先级,优先级为250
priorityMedium: 设置中优先级,优先级为500
priorityHigh: 设置高优先级,优先级为750

需要注意的是,使用priorityLow、priorityMedium、priorityHigh的时候。不是.priorityHigh,而是.priorityHigh()

Shorthand

在写代码的时候,可能你会感觉有的东西要加mas_前缀,有的东西又不用加,代码风格不统一,而且加mas_前缀还麻烦。

前面介绍过加mas_前缀主要是在扩展系统类的时候为了避免与原有类冲突,这是Apple推荐的做法。不过目前来说,即使不加mas_前缀,也不会有什么问题。所以Masonry提供了不加mas_前缀的方法,只需要你定义几个宏即可。

1, MAS_SHORTHAND
定义MAS_SHORTHAND宏之后。可以使用UIView,NSArray中不带mas_前缀的makeConstraints,updateConstraints,remakeConstraints。以及UIView中不带mas_前缀的Attribute
2,  MAS_SHORTHAND_GLOBALS
默认的equalTo方法只接受id类型的对象。有时候我们想传入一个CGFloat, CGSize, UIEdgeInsets等。还需要将其转化成NSValue对象,比较麻烦。Masonry也考虑到了这种情况。只需要定义MAS_SHORTHAND_GLOBALS宏。就可以直接对equalTo传入基础类型。Masonry自动转化成NSValue对象

注意:#define MAS_SHORTHAND和#define MAS_SHORTHAND_GLOBALS这两个宏要在导入Masonry之前定义

第三方 Masonry约束的使用相关推荐

  1. IOS之Masonry约束的使用

    IOS之Masonry约束的使用 Masonry是做约束的,类似苹果开发的约束,做屏幕的适配,有xib开发,纯代码开发,storyboard约束等其他第三方框架. pod 加入 pod 'Masonr ...

  2. Masonry约束自定义TableViewCell自适应行高的约束冲突的问题

    问题@iOS-zhouyu类似 iOS Masonry约束自定义TableViewCell自适应行高的约束冲突的问题_上进求知,认真思辨的博客-CSDN博客_masonry tableviewcell ...

  3. 使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)...

    直接上代码: 1:先自定义cell .h文件中 #import <UIKit/UIKit.h> #import "LBDNewMsgListModel.h" #impo ...

  4. iOS第三方-Masonry使用技巧

    Masonry使用技巧 masonry git地址:https://github.com/SnapKit/Masonry 本文主要会讲到masonry英文文档(见上面的git地址)中提及到的使用说明, ...

  5. Masonry约束崩溃

    报错: Trapped uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint const ...

  6. iOS动画 Masonry约束弹框动画animateWithDuration:

    [self.containtView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(0); ...

  7. 一起来学Masonry (一)

    在看这篇文章之前作者已经默认了你已经有了自动布局的概念,如果对自动布局还是不了解的话,可以看下这篇文章:iOS使用autolayout和sizeclass 解决适配问题 .或许读完这篇文章的话你会对自 ...

  8. iOS-iOS下的Masonry适配

    iOS适配分为两Auto-Layout和Masonry两种,Masonry就是基于Auto-Layout进行封装的第三方约束库, Auto-Layout毕竟是原生的约束库,而Masonry有时因为约束 ...

  9. iOS开发之Masonry框架-使用方法须知

    目录 常用的属性与常量 Masonry使用注意 约束的优先级 Masonry添加约束的方法 修改约束 在哪创建我的约束 创建约束技巧: Masonry是一个轻量级的布局框架,它拥有自己的描述语法(采用 ...

最新文章

  1. 谨慎能捕千秋蝉(二)——CSRF
  2. 矩阵迹的性质_“拨开迷雾”,如何判定矩阵相似?
  3. 封装CoreGraphics的API简化绘图操作
  4. Git子模块引用外部项目
  5. power bi tutorial within Unilever
  6. 简单实现顶部固定,中部自适应布局
  7. Linux ping命令、Linux kill命令、Linux logname命令、 Linux logout命令
  8. 51与PC通信协议设计及实现(三):51部分模块化分工及设计
  9. NeurIPS 2019丨深度双线性转换改进细粒度图像分类
  10. python能做什么项目-python适合什么开发
  11. gdi win7奔溃_gdiplus.dll 32/64位
  12. Docker之LNMP分布式容器部署
  13. SSM搭建-Spring之bean的属性值XML注入方式(4)
  14. 尚学堂视频笔记四:常用类
  15. Java 16进制求和
  16. elasticsearch springboot 实现分词搜索
  17. android ftp权限,实战android手机telnet、ftp登陆,权限修改
  18. 【编程语录】59条令人捧腹但真实的程序员编程语录
  19. SVN客户端安装及使用说明
  20. 快充芯片IP5328P的寄存器数据读写[用于DIY数显快充充电宝]

热门文章

  1. AttributeError: module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘
  2. C语言中不同类型的运算和比较问题
  3. 鸿蒙系统 美的,美的九阳搭载鸿蒙系统的家电上市了,这手机系统上市真的稳了...
  4. 本地jar运行在docker中的方法
  5. 单目视觉技术、双目视觉技术、多目视觉技术
  6. torch.flatten、np.flatten 详解
  7. HSI、HSV、RGB、CMYK、HSL、HSB、Ycc、XYZ、Lab、YUV颜色模型的区别
  8. b2b2c 电子商务平台涉及的技术、运营方案
  9. Stanford Named Entity Recognizer (NER) 斯坦福命名实体识别(NER)
  10. JavaScript 练手小技巧:页面高亮操作提示和蒙板