Masonry的简单使用
2016-11-14 本文已影响188人
云海长天
实例化 redView 和 blueView
//实例化一个view
UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
//masonry自动帮我们把autoresizing给禁用掉
设置 redView 和 blueView 的约束
// 设置redView的约束
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.left.offset(20);
// 针对topLayoutGuide进行设置
// self.mas_bottomLayoutGuide 设置底部
// self.mas_topLayoutGuideBottom 设置顶部
make.top.equalTo(self.mas_topLayoutGuideBottom);
make.left.offset(20);
make.right.offset(20);
make.height.equalTo(@40);
}];
// 设置blueView的约束
/*
dividedBy: 除以
multipledBy:乘以
*/
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(redView.mas_bottom).offset(20);
make.right.offset(-20);
make.height.equalTo(redView);
// 宽度
// make.width.equalTo(redView).multipliedBy(0.5);
make.width.equalTo(redView).dividedBy(2);
}];
```
更新约束
// 更新约束
[redView mas_updateConstraints:^(MASConstraintMaker *make) {
// 更新redView的约束高度变为80
make.height.equalTo(@80);
}];
重新设置约束
// 重新设置,会把之前的约束给清空掉,然后使用新的约束
[redView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.left.offset(20);
make.right.offset(-20);
make.height.equalTo(@80);
}];
设置约束的优先级
// 设置约束的优先级
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.offset(20);
make.right.offset(-20);
make.height.equalTo(@40).priority(10);
}];
/*
priority: 可以设置任意的优先级,接受的参数是0-1000的数字
priorityLow: 设置低优先级,优先级为250
priorityMedium: 设置中优先级,优先级为500
priorityHigh: 设置高优先级,优先级为750
需要注意的是,使用priorityLow、priorityMedium、priorityHigh的时候。不是.priorityHigh,而是.priorityHigh()
*/