【原】iOS學習之Masonry第三方約束,iosmasonry
1、Masonry概述
用優雅的代碼方式編寫Autolayout
省去了蘋果官方噁心的Autolayout代碼
大大提高了開發效率
- 架構地址:https://github.com/SnapKit/Masonry
2、常用方法
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {}];
[blueView mas_remakeConstraints:^(MASConstraintMaker *make) { }];
[blueView mas_updateConstraints:^(MASConstraintMaker *make) { }];
3、約束類型
width(寬)\height(高)\size(大小)
// 寬度約束 make.width.mas_equalTo(100); // 高度約束 make.height.mas_equalTo(100); // 大小約束(與上面兩句等價) 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);
center\centerX\centerY
// 置中(水平+垂直) // 尺寸是父控制項的一半 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(self.view).multipliedBy(0.5); make.center.mas_equalTo(self.view); // 與下面兩句代碼等價// make.centerX.mas_equalTo(self.view);// make.centerY.mas_equalTo(self.view); }];
edges
// UIEdgeInsets 內邊距make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
4、mas_首碼修飾與不修飾的區別
預設情況下:
mas_equalTo有自動封裝功能,比如自動將20封裝為@20
equalTo沒有自動封裝功能
mas_equalTo的功能強於 > equalTo,可以一直使用mas_equalTo
預設情況下:
width是make對象的一個屬性,用來添加寬度約束用的,表示對寬度進行約束
mas_width是一個屬性值,用來當做equalTo的參數,表示某個控制項的寬度屬性
mas_height、mas_centerX以此類推
如果添加了下面的宏,那麼 mas_equalTo 和 equalTo 就沒有區別
#define MAS_SHORTHAND_GLOBALS // 注意:這個宏一定要添加到#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]; // 藍色控制項 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、可有可無的用法
以下方法都僅僅是為了提高可讀性,可有可無
- (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); }];
- (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); }];