深入理解IOS控件布局之Masonry布局框架
前言:
回想起2013年做iOS開發(fā)的時候,那時候并沒有采用手寫布局代碼的方式,而是采用xib文件來編寫,如果使用純代碼方式是基于window的size(320,480)計算出一個相對位置進行布局,那個時候windows的size是固定不變的,隨著iphone5的發(fā)布,windows的size(320,568)也發(fā)生了變化,而采用autoresizingMask的方式進行適配,到后來iphone 6之后windows size的寬度也隨之變化,開始拋棄autoresizingMask改用autolayout了,使用autolayout進行適配我也是最近重新做iOS開發(fā)才接觸的,公司使用Masonry框架進行布局適配。所以學習使用這個布局框架對我來說至關重要,它大大提高了開發(fā)效率而且最近使用起來很多語法和Android有很大的相似之處。
什么是Masonry?
Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優(yōu)雅的鏈式語法封裝自動布局、簡潔明了、 并具有高可讀性、 而且同時支持 iOS 和 Max 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提供了三個函數(shù)方法
- - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增約束
- - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新約束
- - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有約束,只會保留最新的約束
我們根據(jù)不同的使用場景來選擇使用不同的函數(shù)方法。
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
上面的舉例中分別使用了mas_equalTo和equalTo達到了同樣的效果,我在剛開始使用Masonry的時候很容易混淆他們兩個,今天特意分析一下他們的區(qū)別。mas_equalTo是一個MACRO,比較的是值,equalTo比較的是id類型。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS開發(fā)之攔截URL轉換成本地路由模塊URLRewrite詳解
這篇文章主要給大家介紹了關于iOS開發(fā)之攔截URL轉換成本地路由模塊URLRewrite的相關資料,這是最近在工作中遇到的一個需求,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編來一起看看吧。2017-08-08
iOS開發(fā)中CAlayer層的屬性以及自定義層的方法
這篇文章主要介紹了iOS開發(fā)中CAlayer層的屬性以及自定義層的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11

