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:系統重寫佈局


佈局

相關文章
相關標籤/搜索