https://github.com/SnapKit/Masonry
https://www.cnblogs.com/qitiandasheng/p/5462085.html
/** mansonry加上这两个宏就不需要考虑父控件了*/
define MAS_SHORTHAND
define MAS_SHORTHAND_GLOBALS
http://www.jianshu.com/p/yuCytg
[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
/* 等价于
make.top.equalTo(sv).with.offset(10);
make.left.equalTo(sv).with.offset(10);
make.bottom.equalTo(sv).with.offset(-10);
make.right.equalTo(sv).with.offset(-10);
*/
/* 也等价于
make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
*/
}];
作者:里脊串
链接:http://www.jianshu.com/p/yuCytg
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1、Masonry有三个最基本的使用方法###
/** 添加约束 */
[view mas_makeConstraints:<#^(MASConstraintMaker *make)block#>]
/** 更新特定的约束 */
[view mas_updateConstraints:<#^(MASConstraintMaker *make)block#>]
/** 重置所有的约束 */
[view mas_remakeConstraints:<#^(MASConstraintMaker *make)block#>]
就拿第一个方法来举例,其余方法皆雷同
[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));
}];
2、 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.等于: .equalTo
2.小于: .lessThanOrEqualTo
3.大于: .greaterThanOrEqualTo
注意了,这个后面跟的参数分为以下三类
- 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 |
- UIView/NSView
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
- 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));
- NSArray
make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);
- 还有一个prioritize属性的东西,是用来设置优先级
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);
- 还有对于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))
// make width and height greater than or equal to titleLabelmake.size.greaterThanOrEqualTo(titleLabel)
// make width = superview.width + 100, height = superview.height - 50
make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))
// make centerX and centerY = button1make.center.equalTo(button1)
// make centerX = superview.centerX - 5, centerY = superview.centerY + 10
make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))
3、ios有一个方法是用来更新或者增加约束的时候调用的###
- (void)updateConstraints {
[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
//...
}];
//这个父类方法一定要调用,不然会报错
[super updateConstraints];
}
如果你想要主动的调用这个系统方法
[self.btn setNeedsUpdateConstraints];
告诉系统,需要改变约束
当然如果你需要实现动画,那么就只需在动画里面执行
[self.btn layoutIfNeed];
注意:在动画里面改变约束是没有动画效果的。