三、Masonry设置约束的几个示例
1、设置视图与其父视图的边距约束
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
label = [[UILabel alloc]init];
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 20, 20, 20));
}];
label.backgroundColor = [UIColor redColor];
}
设置上下左右与其父视图边距为20px,效果如下:
2、约束控件的尺寸为固定值
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@200);
make.width.equalTo(@200);
make.center.equalTo(self.view);
}];
位置约束设置在了屏幕的中间,效果如下:
3、约束控件之间的尺寸
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@100);
make.width.equalTo(label2);
make.right.equalTo(label2.mas_left).offset(-100);
make.leading.equalTo(self.view.mas_leading).offset(20);
make.centerY.equalTo(self.view);
}];
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@100);
make.centerY.equalTo(label);
make.trailing.equalTo(self.view.mas_trailing).offset(-20);
}];
设置了两个label宽度一致,相距100px,分别距离左右边距20px,效果如下: