1. Masonry的属性
@property (nonatomic,strong,readonly)MASConstraint *left; //左侧
@property(nonatomic,strong,readonly) MASConstraint *top;//上侧
@property(nonatomic,strong,readonly)MASConstraint*right;//右侧
@property(nonatomic,strong,readonly)MASConstraint*bottom; //下侧
@property(nonatomic,strong,readonly)MASConstraint*leading; //首部
@property(nonatomic,strong,readonly) MASConstraint *trailing; //尾部
@property(nonatomic, strong, readonly) MASConstraint *width; //宽
@property (nonatomic, strong, readonly) MASConstraint *height; //高
@property(nonatomic, strong, readonly)MASConstraint *centerX; //横向居中
@property(nonatomic, strong,readonly)MASConstraint *centerY; //纵向居中
@property (nonatomic, strong, readonly) MASConstraint *baseline; //文本基线
2.Masonry给我们提供了3个方法
//新增约束
-
(NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
//更新约束
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker
*make))block;
//清楚之前的所有约束,只会保留最新的约束
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker
*make))block;
3.常见约束的各种类型
1.尺寸:width、height、size
2.边界:left、leading、right、trailing、top、bottom
3.中心点:center、centerX、centerY
4.边界:edges
5.偏移量:offset、insets、sizeOffset、centerOffset
6.priority()约束优先级(0~1000),multipler乘因数,
dividedBy除因数
4.Masonry约束易忽略的技术点
使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraints属性为NO;
防止block中的循环引用,使用弱引用(这是错误观点),在这里block是局部的引用,block内部引用self不会造成循环引用的
__weak typeof
(self) weakSelf = self;(没必要的写法)
5.Masonry约束控件出现冲突的问题
当约束冲突发生的时候,我们可以设置view的key来定位是哪个view
redView.mas_key =
@"redView";
greenView.mas_key
= @"greenView";
blueView.mas_key =
@"blueView";
若是觉得这样一个个设置比较繁琐,怎么办呢,Masonry则提供了批量设置的宏MASAttachKeys
MASAttachKeys(redView,greenView,blueView);
//一句代码即可全部设置
6. equalTo 和 mas_equalTo的区别
mas_equalTo只是对其参数进行了一个BOX(装箱) 操作,目前支持的类型:数值类型(NSNumber)、 点(CGPoint)、大小(CGSize)、边距(UIEdgeInsets),而equalTo:这个方法不会对参数进行包装。
7.Masonry 布局
make.top.equalTo(view).with.offset(10);//
距离上10
make.left.equalTo(view).with.offset(10);//距离左10
make.bottom.equalTo(view).with.offset(-10);//距离下10
make.right.equalTo(view).with.offset(-10);//距离右10
等同于make.edges.mas_offset(UIEdgeInsetsMake(10,10,10,10));
等高 \等宽
make.height.mas_equalTo(@[redView, blueView]);
make.width.mas_equalTo(@[redView, blueView]);
最大值
make.width.height.lessThanOrEqualTo(@250);
最大放大到整个view
make.width.height.lessThanOrEqualTo(self.view);
最小值make.width.height.greaterThanOrEqualTo(@90);
优先级最低
make.width.height.mas_equalTo(100
* self.scacle).priorityLow();
设置高/宽为3:1,要求是同一个控件的属性比例
make.height.mas_equalTo(bottomInnerView.mas_width).multipliedBy(3);
*
axisType 轴线方向
*
fixedSpacing 间隔大小
*
fixedItemLength 每个控件的固定长度/宽度
*
leadSpacing 头部间隔
*
tailSpacing 尾部间隔
//首先添加5个视图
NSMutableArray *array = [NSMutableArray new];
for (int i = 0; i < 5; i ++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor
greenColor];
[self addSubview:view];
[array addObject:view]; //保存添加的控件
}
水平方向控件间隔固定等间隔
[array
mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15
leadSpacing:10 tailSpacing:10];
[array
makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(50);
make.height.equalTo(70);
}];
水平方向宽度固定等间隔
[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
withFixedItemLength:70 leadSpacing:10 tailSpacing:10];
[array
makeConstraints:^(MASConstraintMaker *make) { //数组额你不必须都是view
make.top.equalTo(50);
make.height.equalTo(70);
}];
设置preferredMaxLayoutWidth: 多行label约束的完美解决
[self.label
makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(10);
make.right.equalTo(-10);
}];
更新约束
mas_updateConstraints
// 告诉self.view约束需要更新
[self.view setNeedsUpdateConstraints];
// 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
- (void)updateViewConstraints {
[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
}];
[super updateViewConstraints];
}