您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

深入理解 iOS 控件布局:Masonry 框架完全解析

編輯:iOS 開發綜合

前言:

回想起 2013 年做 iOS 開發時,我並沒有採用純手寫布局代碼的方式,而是用 xib 檔來編排介面。如果走純代碼路線,就得依 window 的 size(320,480) 去算相對位置——那個年代螢幕尺寸是固定的。隨著 iPhone 5 發布,size 變成 (320,568),只能靠 autoresizingMask 硬湊;到了 iPhone 6 之後寬度也開始變化,於是大夥乾脆丟掉 autoresizingMask,全面改用 Auto Layout。我這次重拾 iOS 開發才真正接觸它,而公司用的是 Masonry 框架。老實說,這個框架大幅提升了開發效率,而且很多語法寫起來和 Android 有幾分神似。

什麼是 Masonry?

Masonry 是一個輕量級的布局框架,擁有自己的描述語法,用更優雅的鏈式寫法封裝自動布局,簡潔易讀,同時支援 iOS 與 Mac OS X。

如何使用?

1)引入頭檔

我是在全局的 pch 檔中引入的:

 #import "Masonry.h"

2)基本語法

Masonry 提供的邊%) 屬性:

  • @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;//%) 文本基線

Masonry 提供三個方法:

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增%) 約束
  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新約束
  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清除之前的所有約束,只保留最新%) 的

依不同場景選擇對應的方法即可。

3)具體舉例

比如往父視圖中新增一個上下左右間距皆為 50 的子視圖:

添加約束

 UIView *tempView=[[UIView alloc]init]; tempView.backgroundColor=[UIColor greenColor]; [self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(50); make.right.mas_equalTo(-50); make.top.mas_equalTo(50); make.bottom.mas_equalTo(-50); }];

等價於

 UIView *tempView=[[UIView alloc]init]; tempView.backgroundColor=[UIColor greenColor]; [self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.mas_left).offset(50); make.right.equalTo(self.view.mas_right).offset(-50); make.top.equalTo(self.view.mas_top).offset(50); make.bottom.equalTo(self.view.mas_bottom).offset(-50); }];

也可以簡化為:

 UIView *tempView=[[UIView alloc]init]; tempView.backgroundColor=[UIColor greenColor]; [self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50)); }];

又等價於

 UIView *tempView=[[UIView alloc]init]; tempView.backgroundColor=[UIColor greenColor]; [self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50)); }];

更新約束

 [tempView mas_updateConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(50); make.right.mas_equalTo(-50); make.top.mas_equalTo(100); make.bottom.mas_equalTo(-100); }];

清除舊約束、只保留最新的

 [tempView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(100); make.right.mas_equalTo(-100); make.top.mas_equalTo(100); make.bottom.mas_equalTo(-100); }];

特別注意:聲明約束必須在視圖%) 加入到父視圖之後呼叫,否則會!」 出崩潰或約束無效。

4)mas_equalTo 與 equalTo 的差別

上面的例子中兩者達到了相同效果,我剛開始用 Masonry 時經常混淆。關鍵在於:mas_equalTo 是一個マクロ,比較的是「值」(%) 可以傳入 NSNumber、CGSize、UIEdgeInsets 等);而 equalTo 比較的是 id 類型,兩端必須是") 物。實務上,當你要約束的是數字或結構體時用 mas_equalTo,當") 另一端是視圖或") 屬性時用 equalTo,這樣就不容易出錯。

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved