【原】iOS學(xué)習(xí)之Masonry第三方約束1、Masonry概述
目前最流行的Autolayout第三方框架用優(yōu)雅的代碼方式編寫Autolayout
省去了蘋果官方惡心的Autolayout代碼
大大提高了開發(fā)效率
框架地址:https://github.com/SnapKit/Masonry2、常用方法
這個(gè)方法只會(huì)添加新的約束[blueView mas_makeConstraints:^(MASConstraintMaker *make) {}];這個(gè)方法會(huì)將以前的所有約束刪掉,添加新的約束 [blueView mas_remakeConstraints:^(MASConstraintMaker *make) { }];這個(gè)方法將會(huì)覆蓋以前的某些特定的約束 [blueView mas_updateConstraints:^(MASConstraintMaker *make) { }]; 3、約束類型
尺寸: width(寬)/height(高)/size(大小)

// 寬度約束 make.width.mas_equalTo(100); // 高度約束 make.height.mas_equalTo(100); // 大小約束(與上面兩句等價(jià)) make.size.mas_equalTo(CGSizeMake(100, 100));
邊界: left/leading(左邊界)/right/trailing(右邊界)/top(頂部邊界)/bottom(底部邊界)

// 左邊(leading類似) make.left.mas_equalTo(self.view).offset(50); // 右邊(trailing類似) make.right.equalTo(self.view).offset(-20); // 頂部 make.top.equalTo(self.view).offset(20); // 底部 make.bottom.mas_equalTo(self.view).offset(-50);
中心點(diǎn): center/centerX/centerY

// 居中(水平+垂直) // 尺寸是父控件的一半 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(self.view).multipliedBy(0.5); make.center.mas_equalTo(self.view); // 與下面兩句代碼等價(jià)// make.centerX.mas_equalTo(self.view);// make.centerY.mas_equalTo(self.view); }];
內(nèi)邊距實(shí)現(xiàn)邊界約束: edges
// UIEdgeInsets 內(nèi)邊距make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));4、mas_前綴修飾與不修飾的區(qū)別
mas_equalTo和equalTo 默認(rèn)情況下:
mas_equalTo有自動(dòng)包裝功能,比如自動(dòng)將20包裝為@20
equalTo沒有自動(dòng)包裝功能
mas_equalTo的功能強(qiáng)于 > equalTo,可以一直使用mas_equalTo
mas_width和width 默認(rèn)情況下:
width是make對(duì)象的一個(gè)屬性,用來添加寬度約束用的,表示對(duì)寬度進(jìn)行約束
mas_width是一個(gè)屬性值,用來當(dāng)做equalTo的參數(shù),表示某個(gè)控件的寬度屬性
mas_height、mas_centerX以此類推
消除區(qū)別辦法 如果添加了下面的宏,那么 mas_equalTo 和 equalTo 就沒有區(qū)別
#define MAS_SHORTHAND_GLOBALS // 注意:這個(gè)宏一定要添加到#import "Masonry.h"前面 如果添加了下面的宏,mas_width也可以寫成width
#define MAS_SHORTHAND

//define this constant if you want to use Masonry without the 'mas_' PRefix#define MAS_SHORTHAND//define this constant if you want to enable auto-boxing for default syntax#define MAS_SHORTHAND_GLOBALS#import "Masonry.h" - (void)viewDidLoad { [super viewDidLoad]; // 藍(lán)色控件 UIView *blueView = [[UIView alloc] init]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; // 紅色控件 UIView *redView = [[UIView alloc] init]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; // 添加約束 CGFloat margin = 20; CGFloat height = 50; [blueView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left).offset(margin); make.right.equalTo(redView.left).offset(-margin); make.bottom.equalTo(self.view.bottom).offset(-margin); make.height.equalTo(height); make.top.equalTo(redView.top); make.bottom.equalTo(redView.bottom); make.width.equalTo(redView.width); }]; [redView makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.right).offset(-margin); }];} 
5、可有可無的用法
以下方法都僅僅是為了提高可讀性,可有可無
with- (MASConstraint*)with { return self;} 使用情況示例代碼
// 尺寸限制:100x100 // 位置:粘著父控件右下角,間距是20 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // 寬度約束 make.width.equalTo(@100); // 高度約束 make.height.equalTo(@100); // 右邊 make.right.equalTo(self.view.mas_right).with.offset(-20); // 頂部 make.top.equalTo(self.view.mas_top).with.offset(20); }];
and- (MASConstraint*)and { return self;} 使用情況示例代碼

// 尺寸限制:100x100 // 位置:粘著父控件右下角,間距是20 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { // 寬度高度約束 make.width.and.height.mas_equalTo(100); // 右邊 make.right.equalTo(self.view).offset(-20); // 頂部 make.top.equalTo(self.view).offset(20); }];
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注