masonry学习

2017-03-24  本文已影响25人  心情的蛊惑
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; //文本基线

使用:

1,先创建一个View

UIView *sv = [UIView new];

sv.backgroundColor = [UIColor blackColor];

2,添加到父视图上

//autolayout之前,先将View添加到supview上

[self.view addSubview:sv];

3,设置约束

[sv mas_makeConstraints:^(MASConstraintMaker *make) {

//设置sv的中心

make.center.equalTo(ws.view);

//设置sv的宽高

make.size.mas_equalTo(CGSizeMake(200, 200));

}];

4,创建了一个新的View,添加到上一个view上,并设置约束

UIView *sv1 = [UIView new];

sv1.backgroundColor = [UIColor redColor];

[sv addSubview:sv1];

[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {

//设置各个方向的缩进(三种方法)

(1)// make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 12, 10));

(2)/*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);(右)*/

(3)make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

}];

注意:make.right.equalTo(sv).with.offset(10);

make.right.equalTo(sv.mas_left).with.offset(10);

这两行表示含义不同,前者是相对sv的右边,后者是相对sv的左边.也就是说当equalTo后面没写相对哪一边时默认和make.后面的方向一致.

/*

mas_makeConstraints只负责新增的约束,autoLayout不能同时存在两条针对同一对象的约束,负责会报错

mas_updateConstraints针对上面的情况,会更新在block中出现的约束,不会导致出现两个同一约束的情况,必须针对同一个参照对象,使用时必须加上[super updateViewConstraints];这句代码

mas_remakeConstraints则会清除之前所有的约束,仅仅保留最新的约束,block里面就是你所要添加的新约束.

*/

上一篇下一篇

猜你喜欢

热点阅读