Masonry
Masonry是目前最流行的Autolayout第三方框架,省去了蘋果官方惡心的Autolayout代碼,大大提高了開發(fā)效率。
mas_equalTo和equalTo
- 默認(rèn)情況下mas_equalTo有自動(dòng)包裝功能,比如自動(dòng)將整型常量30包裝為@30,equalTo沒有自動(dòng)包裝功能
- 如果添加了下面的宏,那么mas_equalTo和equalTo就沒有區(qū)別:#define MAS_SHORTHAND_GLOBALS(注意:這個(gè)宏一定要添加到#import"Masonry.h"前面否則將沒有效果)。
mas_width和width
默認(rèn)情況下width是make對象的一個(gè)屬性,用來添加寬度約束用的,表示對寬度進(jìn)行約束mas_width是一個(gè)屬性值,用來當(dāng)做equalTo的參數(shù),表示某個(gè)控件的寬度屬性
如果添加了下面的宏,mas_width也可以寫成width
defineMAS_SHORTHAND
mas_height、mas_centerX以此類推
頭部添加
#import "ViewController.h"
//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"
- 代碼部分
// 藍(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);
}];
最終顯示效果

豎屏效果

橫屏效果