分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
Masonry有三个最基本的使用方法
/** 添加约束 */
[view mas_makeConstraints:<#^(MASConstraintMaker *make)block#>]
/** 更新特定的约束 */
[view mas_updateConstraints:<#^(MASConstraintMaker *make)block#>]
/** 重置所有的约束 */
[view mas_remakeConstraints:<#^(MASConstraintMaker *make)block#>]
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
就拿第一个方法来举例,其余方法皆雷同
[redView mas_makeConstraints:^(MASConstraintMaker *make{
make.top.equalTo(superView.mas_top).with.offset(10);
make.right.equalTo(superView.mas_right).with.offset(-10);
make.size.equalTo(@(100));
}];
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
block里面的写法有很多种,属性也有很多种
我就把一些基础的列在下面:
下面这些是基本的设置属性
/**
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除因数
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
有三种关系:等于,小于,大于
.equalTo
.lessThanOrEqualTo
.greaterThanOrEqualTo
注意了,这个后面跟的参数分为以下三类
1. MASViewAttribute
MASViewAttribute | NSLayoutAttribute |
---|---|
view.mas_left | NSLayoutAttributeLeft |
view.mas_right | NSLayoutAttributeRight |
view.mas_top | NSLayoutAttributeTop |
view.mas_bottom | NSLayoutAttributeBottom |
view.mas_leading | NSLayoutAttributeLeading |
view.mas_trailing | NSLayoutAttributeTrailing |
view.mas_width | NSLayoutAttributeWidth |
view.mas_height | NSLayoutAttributeHeight |
view.mas_centerX | NSLayoutAttributeCenterX |
view.mas_centerY | NSLayoutAttributeCenterY |
view.mas_baseline | NSLayoutAttributeBaseline |
2. UIView/NSView
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
- 1
- 2
- 1
- 2
- NSNumber/各种结构体 : @100
make.width.greaterThanOrEqualTo(@200);
make.size.mas_equalTo(CGSizeMake(50, 100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- NSArray
make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);
- 1
- 2
- 3
- 1
- 2
- 3
还有一个prioritize属性的东西,是用来设置优先级
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);
- 1
- 2
- 1
- 2
还有对于edges、size、center的使用
// make top, left, bottom, right equal view2
make.edges.equalTo(view2);
// make top = superview.top + 5, left = superview.left + 10,
// bottom = superview.bottom - 15, right = superview.right - 20
make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
// make width and height greater than or equal to titleLabel
make.size.greaterThanOrEqualTo(titleLabel)
// make width = superview.width + 100, height = superview.height - 50
make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
// make centerX and centerY = button1
make.center.equalTo(button1)
// make centerX = superview.centerX - 5, centerY = superview.centerY + 10
make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
ios有一个方法是用来更新或者增加约束的时候调用的
- (void)updateConstraints {
[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
//...
}];
//这个父类方法一定要调用,不然会报错
[super updateConstraints];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
如果你想要主动的调用这个系统方法
[self.btn setNeedsUpdateConstraints];
- 1
- 1
告诉系统,需要改变约束
当然如果你需要实现动画,那么就只需在动画里面执行
[self.btn layoutIfNeed];
- 1
- 1
注意:在动画里面改变约束是没有动画效果的。