iOS下的Masonry适配

iOS适配分为两Auto—Layout和Masonry两种,Masonry就是基于Auto—Layout进行封装的第三方约束库,

Auto—Layout毕竟是原生的约束库,而Masonry有时因为约束不好,总会出现一些警告,虽然不会导致程序崩溃。

本人根据UI所给标注图加上公司要求,目前使用纯代码开发。所以,给出本人对Masonry的理解
[objc] view plain copy
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;

扫描二维码关注公众号,回复: 1109456 查看本文章

// 首部

@property (nonatomic,strong,readonly) MASConstraint *leading;

// 尾部

@property (nonatomic,strong,readonly) MASConstraint *trailing;

// 宽

@property (nonatomic,strong,readonly) MASConstraint *width;

// 高

@property (nonatomic,strong,readonly) MASConstraint *height;

// 中心点x

@property (nonatomic,strong,readonly) MASConstraint *centerX;

// 中心点y

@property (nonatomic,strong,readonly) MASConstraint *centerY;

// 文本基线

@property (nonatomic,strong,readonly) MASConstraint *baseline;

这些属性与NSLayoutAttrubute的对照表如下

43.jpg

2、Masonry的三个函数

//设置约束
- (NSArray )mas_makeConstraints:(void(^)(MASConstraintMaker ))block;
//如果之前已经有约束,则更新新的约束,如果没有约束,则添加约束
- (NSArray )mas_updateConstraints:(void(^)(MASConstraintMaker ))block;
//将之前的约束全部删除,添加新的约束

  • (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

equal和mas_equal区别
masequalTo 比equalTo多了类型转换操作,一般来说,大多数时候两个方法都是 通用的,
但是对于数值元素使用mas_equalTo。对于对象或是多个属性的处理,使用equalTo。
特别是多个属性时,必须使用equalTo,例如 make.left.and.right.equalTo(self.view);

3、使用Masonry注意事项
UIView *sv = [UIView new];
//在做autoLayout之前 一定要先将view添加到superview上 否则会崩溃(报错为:不能找到父视图)
[self.view addSubview:sv];
//mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了

4、一般情况下,masonry约束使用
(1)设置边距
make.edges.equalTo(self.view)//.with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
(2)设置中心,大小
make.center.equalTo(ws.view);
//将size设置成(300,300)
make.size.mas_equalTo(CGSizeMake(300, 300));

(3)设置上、下、左、右(或者设置上、左,宽、高)

【1】 make.top.equalTo(v_scrollTimer.scrollView.mas_bottom);

    make.height.mas_equalTo(@(f_CalcRealHeightByiPhone6(170));  

    make.left.equalTo(backView.mas_left);  

    make.right.equalTo(backView.mas_right);  

【2】 make.centerX.equalTo(img_welcome.mas_centerX);

    make.width.mas_equalTo(@(15));  

    make.height.mas_equalTo(@(18));  

    make.bottom.equalTo(img_welcome.mas_bottom).offset(-5);  

猜你喜欢

转载自blog.csdn.net/yyacheng/article/details/52587813