1沧奴、常用方法:
這個(gè)方法只會(huì)添加新的約束
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
}];
這個(gè)方法會(huì)將以前的所有約束刪掉臣镣,添加新的約束
[blueView mas_remakeConstraints:^(MASConstraintMaker *make) {
}];
這個(gè)方法將會(huì)覆蓋以前的某些特定的約束
[blueView mas_updateConstraints:^(MASConstraintMaker *make) {
}];
2、約束類(lèi)型
尺寸:
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類(lèi)似)
make.left.mas_equalTo(self.view).offset(50);
// 右邊(trailing類(lèi)似)
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)邊界約束:
// UIEdgeInsets 內(nèi)邊距
make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
3航背、mas_前綴修飾與不修飾的區(qū)別
mas_equalTo和equalTo
默認(rèn)情況下:
mas_equalTo有自動(dòng)包裝功能,比如自動(dòng)將20包裝為@20
equalTo沒(méi)有自動(dòng)包裝功能
mas_equalTo的功能強(qiáng)于 > equalTo巫击,可以一直使用mas_equalTo
#import "ViewController.h"
#import "Masonry/Masonry.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *A;
@property (nonatomic, strong) UIView *B;
@property (nonatomic, strong) UIView *C;
@property (nonatomic, strong) UIView *D;
@property (nonatomic, strong) UIView *E;
@property (nonatomic, strong) UIView *F;
@property (nonatomic, strong) UIView *G;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_A = [[UIView alloc]init];
_A.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_A];
_B = [[UIView alloc]init];
_B.backgroundColor = [UIColor blueColor];
[self.view addSubview:_B];
_C = [[UIView alloc]init];
_C.backgroundColor = [UIColor grayColor];
[self.view addSubview:_C];
_D = [[UIView alloc]init];
_D.backgroundColor = [UIColor yellowColor];
[_C addSubview:_D];
_E = [[UIView alloc]init];
_E.backgroundColor = [UIColor blueColor];
[_C addSubview:_E];
_F = [[UIView alloc]init];
_F.backgroundColor = [UIColor redColor];
[_C addSubview:_F];
[_A mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(20);
make.left.mas_equalTo(20);
make.height.mas_equalTo(150);
make.right.equalTo(_B.mas_left).mas_offset(-20);
make.width.mas_equalTo(_B);
}];
[_B mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-20);
make.height.mas_equalTo(150);
make.top.mas_equalTo(20);
make.left.equalTo(_A.mas_right).mas_offset(20);
make.width.mas_equalTo(_A);
}];
[_C mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_A.mas_bottom).mas_offset(20);
make.height.equalTo(_A);
make.left.mas_equalTo(20);
make.right.mas_equalTo(-20);
}];
[_D mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_C).mas_offset(10);
make.left.equalTo(_C).mas_offset(10);
make.right.equalTo(_E.mas_left).mas_offset(-20);
make.height.mas_equalTo(40);
make.width.mas_equalTo(_E);
}];
[_E mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_C).offset(10);
make.right.equalTo(_C).offset(-10);
make.left.equalTo(_D.mas_right).mas_offset(20);
make.height.mas_equalTo(40);
make.width.mas_equalTo(_D);
}];
[_F mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_D.mas_bottom).mas_offset(10);
make.right.equalTo(_C).mas_offset(-10);
make.left.equalTo(_C).mas_offset(10);
make.bottom.equalTo(_C).mas_offset(-10);
}];
}
@end