View中所欲子控件的位置
1.水平布局当前视图中所有子控件
//1.水平方向排列、固定控件间隔、控件长度不定
[self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:5 tailSpacing:5];
2、水平方向排列、固定控件长度、控件间隔不定
[self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:100 leadSpacing:10 tailSpacing:10];
最后设置高度与位置即可
[self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.height.mas_equalTo(70);
}];
设置最大与最小的宽度(高度同理)
200<= wight <= 500
make.width.mas_lessThanOrEqualTo(200);
make.width.mas_greaterThanOrEqualTo(500);
或者以下写法
make.width.lessThanOrEqualTo(@200);
make.width.greaterThanOrEqualTo(@500);
masonry的简洁写法
常规设置约束
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).offset(padding.top);
make.left.equalTo(superview.mas_left).offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).offset(-padding.bottom);
make.right.equalTo(superview.mas_right).offset(-padding.right);
}];
但是也可以一行代码搞定
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).insets(padding);
}];
view.left 大于或等于label.left:
// 下面两个约束是完全等效的.
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
但是自适应布局不支持将 left,right, centerY等设为固定值.
如果你给这些属性传递一个常量, Masonry会自动将它们转换为相对于其父视图的相对值:
make.left.lessThanOrEqualTo(@10)
除了使用 NSNumber 外,你可以使用基本数据类型或者结构体来创建约束:
make.top.mas_equalTo(42);
make.height.mas_equalTo(20);
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));
一个数组,里面可以混合是前述三种类型的任意几种:
// 表达三个视图等高的约束.
make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);
约束的优先级
.priority 允许你指定一个精确的优先级,数值越大优先级越高.最高1000.
.priorityHigh 等价于 UILayoutPriorityDefaultHigh.优先级值为 750.
.priorityMedium 介于高优先级和低优先级之间,优先级值在 250~750之间.
.priorityLow 等价于 UILayoutPriorityDefaultLow, 优先级值为 250.
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);
等比例自适应
// 指定宽度为父视图的 1/4.
make.width.equalTo(superview).multipliedBy(0.25);
Masonry提供了一些工具方法来进一步简化约束的创建.
edges 边界
//使 top, left, bottom, right等于 view2
make.edges.equalTo(view2);
//使 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))
size 尺寸
// 使宽度和高度大于或等于 titleLabel
make.size.greaterThanOrEqualTo(titleLabel)
//使 width = superview.width + 100, height = superview.height - 50
make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))
center 中心
//使 centerX和 centerY = button1
make.center.equalTo(button1)
//使 centerX = superview.centerX - 5, centerY = superview.centerY + 10
make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))
References 引用
把 Masonry 语法返回的约束或约束数组,存储到一个局部变量或者类的属性中,以供后续操作某个约束.
@property (nonatomic, strong) MASConstraint *topConstraint;
...
// when making constraints
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
make.left.equalTo(superview.mas_left).with.offset(padding.left);
}];
...
// 然后你就可以操作这个属性.
[self.topConstraint uninstall];
mas_updateConstraints
注意:如果你只是想添加新的约束,你可以使用便利方法mas_updateConstraints,不需要使用 mas_makeConstraints. mas_updateConstraints,不会移除已经存在的约束(即使新旧约束间相互冲突).
[self.redView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(100);
make.centerX.equalTo(self.view);
make.width.height.mas_equalTo(300);
}];
mas_remakeConstraints
注意:mas_remakeConstraints与mas_updateConstraints相似,不同之处在于: mas_remakeConstraints 会先移除视图上已有的约束,再去创建新的约束.
[self.redView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(200);
make.left.equalTo(self.view).offset(200);
make.size.mas_equalTo(CGSizeMake(300, 150));
}];