Masonry:
一片吊、簡(jiǎn)介:
- 目前最流行的Autolayout第三方框架
- 用優(yōu)雅的代碼方式編寫Autolayout
- 省去了蘋果官方復(fù)雜的Autolayout代碼
- 很大程度提高了開發(fā)效率
二、框架地址:
https://github.com/snapKit/Masonry
三、示例:
//邊距約束:
-(void)test1{
// 1.紅色的View
UIView *redView= [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 2.1添加約束://默認(rèn)mutipliedBy = 1.0 可以不寫
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.view.mas_top).multipliedBy(1.0).offset(20);
// make.bottom.equalTo(self.view.mas_bottom).multipliedBy(1.0).offset(-20);
// make.left.equalTo(self.view.mas_left).multipliedBy(1.0).offset(20);
// make.right.equalTo(self.view.mas_right).multipliedBy(1.0).offset(-20);
// }];
// 2.2可以簡(jiǎn)化:
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.view.mas_top).offset(20);
// make.bottom.equalTo(self.view.mas_bottom).offset(-20);
// make.left.equalTo(self.view.mas_left).offset(20);
// make.right.equalTo(self.view.mas_right).offset(-20);
// }];
// 2.3可以再簡(jiǎn)化:
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.offset(20);
// make.bottom.offset(-20);
// make.left.offset(20);
// make.right.offset(-20);
// }];
// 2.4可以再再簡(jiǎn)化:
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.and.left.offset(20);
// make.bottom.and.bottom.offset(-20);
// }];
// 2.5可以再再再簡(jiǎn)化:
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.left.offset(20);
// make.bottom.bottom.offset(-20);
// }];
// 2.6可以再再再再簡(jiǎn)化:
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 20, 20, 20));
// }];
// 2.7可以再再再再再簡(jiǎn)化:
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.insets(UIEdgeInsetsMake(20, 20, 20, 20));
}];
}
//中心約束:
-(void)test2{
// 1.紅色的View
UIView *redView= [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 2.1添加約束
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(@100);
// make.height.equalTo(@100);
// make.centerX.equalTo(self.view.mas_centerX);
// make.centerY.equalTo(self.view.mas_centerY);
// }];
// 2.2添加約束 : 自動(dòng)轉(zhuǎn)換成相應(yīng)的類型 一個(gè)很龐大的枚舉
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.mas_equalTo(100);
// make.height.mas_equalTo(100);
// make.centerX.mas_equalTo(self.view.mas_centerX);
// make.centerY.mas_equalTo(self.view.mas_centerY);
// }];
// 2.3 添加約束
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.mas_equalTo(self.view);
}];
}
//綜合示例:
-(void)test3{
// 1.紅色的View
UIView *redView= [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 2.藍(lán)色的View
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// // 3.藍(lán)色View的約束
// [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.mas_equalTo(self.view.mas_left).offset(30);
// make.bottom.mas_equalTo(self.view.mas_bottom).offset(-30);
// make.right.mas_equalTo(redView.mas_left).offset(-30);
// make.width.mas_equalTo(redView.mas_width);
// make.height.mas_equalTo(50);
// }];
//
// // 4.紅色View的約束
// [redView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.right.mas_equalTo(self.view.mas_right).offset(-30);
// make.top.mas_equalTo(blueView.mas_top);
// make.bottom.mas_equalTo(blueView.mas_bottom);
// }];
// 3.1藍(lán)色View的約束: 添加宏 可以去掉mas
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(30);
make.bottom.equalTo(self.view.bottom).offset(-30);
make.right.equalTo(redView.left).offset(-30);
make.width.equalTo(redView.width);
make.height.equalTo(50);
}];
// 4.1紅色View的約束: 添加宏 可以去掉mas
[redView makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view.right).offset(-30);
make.top.equalTo(blueView.top);
make.bottom.equalTo(blueView.bottom);
}];
// 更新約束:
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(80);
}];
//刪除之前添加的所有約束伍掀,重新添加下面的約束
// [blueView remakeConstraints:^(MASConstraintMaker *make) {
//
// }];
//with and 可以任意寫 不影響使用
}