iOS经常使用库之Masonry

 简单介绍

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

```
pod 'Masonry'
```

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

 系统API vs Masonry

  系统API

NSLayoutConstraint

```objective-cgit

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

[self.view addSubview:topConstraint];

 

```

 Masonry API

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

```github

[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-cobjective-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;

```less


其中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
ide

@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:系统重写布局


布局

相关文章
相关标签/搜索