MasonryAutoLayout 学习。
注释:左边都是 .left/ .right/.bottom/.top/.centerY/右边都是 :mad_right/mas_bottom/mas_top 等等。
self.reasonLabel = [[UILabel alloc] init];
[self.reasonLabel setBackgroundColor:[UIColor clearColor]];
[self.reasonLabel setTextAlignment:NSTextAlignmentLeft];
[self.reasonLabel setTextColor:[UIColor colorFromHexString:@"999999"]];
[self.reasonLabel setFont:[UIFont systemFontOfSize:12.0]];
[self.reasonLabel setText:@"验证消息"];
[self.contentView addSubview:self.reasonLabel];
///UILabel 的话,不需要设置size。 with.offset(),里面是正数表示向右或者向下,负数的话,表示向左或者向上。
[self.reasonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(wSelf.headerImageView.mas_right).with.offset(10.0);
make.bottom.equalTo(wSelf.contentView.mas_bottom).with.offset(-8);
make.right.equalTo(wSelf.contentView.mas_right).with.offset(-50.0);
}];
///下面这句相当于:当前按钮和cancelButton 的宽度是125:80
// make.width.equalTo(cancelButton.mas_width).with.multipliedBy(125.0 / 80.0);
equalTo 和 mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO,比较的是值,equalTo比较的是view。
小例子二:
UIView *sv1 = [UIView new];
sv1.backgroundColor = [UIColor redColor];
[sv addSubview:sv1];
[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {make.edges.equalTo(superview).with.insets(padding);}];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic fillermake.left.equalTo(superview.mas_left).with.offset(padding.left);make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);make.right.equalTo(superview.mas_right).with.offset(-padding.right);}];
让一个在父视图上上下左右各缩进10个长度(请自动脑补如果使用Autolayout的代码量)
1 //设置约束
2 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block;
3
4 //如果之前已经有约束,则更新新的约束,如果没有约束,则添加约束
5 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block;
6
7 //将之前的约束全部删除,添加新的约束
8 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
//给蓝色View设置约束
13 [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
14 make.left.equalTo(self.view.mas_left).offset(30);//和父view的左边间距为30;
15 make.bottom.equalTo(self.view.mas_bottom).offset(-30);//和父view的底部间距为30;
16 make.right.equalTo(redView.mas_left).offset(-30);//和红色view的间距为30;
17 make.height.mas_equalTo(50);//蓝色view的高度为50
18 }];
19
20 //给红色View设置约束
21 [redView mas_makeConstraints:^(MASConstraintMaker *make) {
22 make.right.equalTo(self.view.mas_right).offset(-30);//和父view的右边间距为30;
23 make.bottom.equalTo(blueView.mas_bottom);//和蓝色view的底部对齐
24 make.height.equalTo(blueView.mas_height);//和蓝色view的高度相等
25 make.width.equalTo(blueView.mas_width);//和蓝色view的宽度相等
26 }];