简单介绍

Masonry 源码地址:https://github.com/Masonry/Masonry
Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Mac OS X。

```
pod 'Masonry'
```

使用Masonry需要导入头文件 `#import  “Masonry.h”`

 系统API vs Masonry

系统API

NSLayoutConstraint

```objective-c

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:item attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:item2 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:];

[self.view addSubview:topConstraint];

```

Masonry API

先来看一段官方的sample code来认识一下Masonry

```

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {

    make.edges.equalTo(superView).with.insets(padding);
}];

```

看到block里面的那句话: `make edges
equalTo superview with insets`
通过链式的自然语言 就把view1给autolayout好了

是不是简单易懂

使用介绍

操作约束方法

Masonry 处理约束主要是`View+MASAdditions`,它是`UIView`的`类别`

```objective-c

// 添加约束
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; // 更新约束, 如果更新不成功有可能该约束不存在
//只能修改约束的值,不能更改约束关系即不能更改核心公式中的两个item和两个property
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; // 重置约束 将原有约束全部删除 重新添加
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

```

MASConstraintMaker属性

Masonry中`MASConstraintMaker`约束对象的 `MASConstraint` 类型属性:

```objective-c

@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;
@property (nonatomic, strong, readonly) MASConstraint *edges;
@property (nonatomic, strong, readonly) MASConstraint *size;
@property (nonatomic, strong, readonly) MASConstraint *center;

```


其中leading与left trailing与right 正常情况下是等价的 但是当一些布局是从右至左时(比如阿拉伯文?没有类似的经验) 则会对调 换句话说就是基本可以不理不用 用left和right就好了

Relation

在 `MASConstraint` 类别中有以下几个方法表示view与view之间关系

```objective-c
- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
```

以上Relation方法相关方法的调用都会返回MASConstraint对象

UIView的扩展属性

```objective-c

@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;


```

MASConstraint相关

```objective-c

// insets:在上左下右四个方向缩进
- (MASConstraint * (^)(MASEdgeInsets insets))insets; // sizeOffset:宽高的偏移量
- (MASConstraint * (^)(CGSize offset))sizeOffset; // centerOffset:中心点偏移量
- (MASConstraint * (^)(CGPoint offset))centerOffset; // offset:偏移量
- (MASConstraint * (^)(CGFloat offset))offset; // valueOffset:value形式的偏移量
- (MASConstraint * (^)(NSValue *value))valueOffset; // multipliedBy:添加约束核心公式中的multiplier
- (MASConstraint * (^)(CGFloat multiplier))multipliedBy; // priority:设置优先级
- (MASConstraint * (^)(MASLayoutPriority priority))priority; // priorityLow:优先级低
- (MASConstraint * (^)())priorityLow; // priorityMedium:优先级中
- (MASConstraint * (^)())priorityMedium; // priorityHigh:优先级高
- (MASConstraint * (^)())priorityHigh; // 表示约束关系的,返回block,后面支持MASViewAttribute, UIView, NSValue, NSArray等类型
- (MASConstraint * (^)(id attr))equalTo; - (MASConstraint * (^)(id attr))greaterThanOrEqualTo; - (MASConstraint * (^)(id attr))lessThanOrEqualTo; // 可选的语义属性,对代码没有影响,只是为了提高代码可读性
- (MASConstraint *)with;
- (MASConstraint *)and; - (MASConstraint *)left;
- (MASConstraint *)top;
- (MASConstraint *)right;
- (MASConstraint *)bottom;
- (MASConstraint *)leading;
- (MASConstraint *)trailing;
- (MASConstraint *)width;
- (MASConstraint *)height;
- (MASConstraint *)centerX;
- (MASConstraint *)centerY;
- (MASConstraint *)baseline;


```

两个简易宏

 为了增加代码的可读性这里有两个简化代码的宏:
   `#define MAS_SHORTHAND`:只要在导入Masonry主头文件之前定义这个宏, 那么以后在使用Masonry框架中的属性和方法的时候, 就可以省略 `mas_` 前缀
   `#define MAS_SHORTHAND_GLOBALS`:只要在导入Masonry主头文件之前定义这个宏,那么就可以让equalTo函数接收基本数据类型, 内部会对基本数据类型进行包装

***注意***:这两个宏如果想有效使用,必须要在添加`Masonry`头文件导入之前定义。

Masonry使用注意事项

1. view在使用masonry添加约束时,view必须已经添加到其父视图上了;
2. label设置换行时,需要设置宽度
3. 动画记得调用layoutIfNeeded
4. mas_makeConstraints 只负责新增约束。Masonry不能同时存在两条针对于同一对象的约束 否则会报错;
5. 在使用三目运算符时,注意不要使用基本数据类型
6. mas_updateConstraints 会更新在block中出现的约束的值,对于之前不存在的约束关系,不会加载
7. iOS7有两个很有用的属性,topLayoutGuide和bottomLayoutGuide,这个两个主要是方便获取UINavigationController和UITabBarController的头部视图区域最下边和底部视图区域的最上边;

AutoLayout关于更新的几个方法的区别

* setNeedsUpdateConstraints:告知约束系统需要更新约束,但是不会立刻开始
* updateConstraintsIfNeeded:告知立刻更新约束
* layoutIfNeeded:告知页面布局立刻更新。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
* updateConstraints:官方建议写添加、更新约束代码的地方,如果重写了此方法,需要在该方法末尾调用[super updateConstraints]
* setNeedsLayout:告知页面需要更新,但是不会立刻开始更新。执行后会立刻调用layoutSubviews。
* layoutSubviews:系统重写布局

iOS常用库之Masonry的更多相关文章

  1. ios 常用库

    SwiftHTTP       网络请求库 SwiftyJSON     json解析库 SnapKit          自动布局库 Kingfisher      图像加载库 WRCycleScr ...

  2. IOS常用第三方库《转》

    UI 动画 网络相关 Model 其他 数据库 缓存处理 PDF 图像浏览及处理 摄像照相视频音频处理 响应式框架 消息相关 版本新API的Demo 代码安全与密码 测试及调试 AppleWatch ...

  3. iOS常用第三方库大全,史上最全第三方库收集

    下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableVie ...

  4. ios常用第三方库git下载地址

    本文转载至 http://blog.csdn.net/cerastes/article/details/38348599 iOS第三方库下载常用git 1.FMDB https://github.co ...

  5. 【iOS系列】-iOS开发常用库文件总结

    这里是列举出得一部分,更多内容可参考https://github.com/darren90/Gather_iOS 码农周刊的总结 - 覆盖很广 调调的 - 很多开发相关内容都有体现 右滑返回的解决 - ...

  6. iOS常用的第三方库GitHub地址

    MJRefresh https://github.com/CoderMJLee/MJRefresh#期待 Facebook-POP https://github.com/facebook/pop /* ...

  7. 一些 iOS 常用的第三方库

    网络通信 AFNetworking 轻量级的通讯类库,使用非常简单.建议更新到最新版,前几天看新闻说之前有个逻辑性的 bug https://github.com/AFNetworking/AFNet ...

  8. iOS常用网络库收集

    一 ASIHttpRequest二 AFNetworking 三 AFDownloadRequestOperationA progressive download operation for AFNe ...

  9. iOS 常用三方类库整理

    iOS 常用三方类库整理 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://gi ...

随机推荐

  1. LRU implement Data Structure analysis

    三种数据结构实现的LRU对比分析: 自适应循环链表, 跳表 和 伸展树 对比发现 : 跳表比其他两个会好一些(命中率) 来自论文 Performance Analysis of LRU

  2. hive导入数据

    替换分隔符为\ sed -i 's/\t/\x1/g;s/;/\x1/g' test1.txt gz压缩 gzip -r test1.txt 查看文件 hdfs dfs -ls /hive/wareh ...

  3. Spring 事务处理

    Spring 默认执行事务回滚:当开启事务的类中对数据库的操作的异常没有任何处理时,才会主动触发事务回滚. 而很多时候业务都需要对抛出的异常进行处理,所以如果try,catch了操作数据库的方法,事务 ...

  4. log4j输出模板

    log4j.rootLogger=DEBUG, A1,A2 log4j.appender.A1.MaxFileSize=1kb#10个备份 log4j.appender.A1.MaxBackupInd ...

  5. phpwind ecshop 用户整合

    phpwind ecshop 用户整合,其实很简单.但在网上搜到的尽是乱七八遭的方法,搞得很复杂. 原来公司做的phpwind 与 ecshop 结合的项目,别的同事已经把用户整合好了,当时我还不知道 ...

  6. 头像上传,拖拽,裁切 (非HTML5)版本

    演示地址: http://codeman35.itongyin.com:19002/v2/web_demo.html 功能: 支持滚轴放大缩小,鼠标拖动,裁切可视区域,裁切和图片处理都是后端操作.

  7. Windows程序设再读笔记03-窗口与消息

    1.关于LoadIcon/LoadCursor,这两个函数,第一个参数为实例句柄,如果是从保存在磁盘中的可执行文件中加载资源,则需要则需要指定可执行文件的hInstance,如果是系统资源,该句柄为N ...

  8. Web应用程序项目XX已配置为使用IIS

    今天在看开源项目Umbraco是,出现一个项目加载不了,并报如下错误: Web应用程序项目Umbraco.Cms.Web.UI已配置为使用IIS.若要访问本地IIS网站,必须安装下列IIS组件..,如 ...

  9. MyEclipse 8.5汉化教程

    汉化包下载:http://yunpan.cn/QIUaVS2CU5wCd 1.解压MyEclipse中的language文件夹 以我的安装目录为例,我的MyEclipse8.5的安装在D:盘下.将解压 ...

  10. Solving “Dynamic Web Module 3.0 requires Java 1.6 or newer” in Maven Projects

    不一定是在Maven Projects里才有这种情况,但解决方法是一样的. 转自:http://qussay.com/2013/09/13/solving-dynamic-web-module-3-0 ...