iOS下的Masonry适配

来源:互联网 发布:数据库 migration 编辑:程序博客网 时间:2024/06/05 14:56
iOS适配分为两Auto—Layout和Masonry两种,Masonry就是基于Auto—Layout进行封装的第三方约束库,
Auto—Layout毕竟是原生的约束库,而Masonry有时因为约束不好,总会出现一些警告,虽然不会导致程序崩溃。
本人根据UI所给标注图加上公司要求,目前使用纯代码开发。所以,给出本人对Masonry的理解
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;// 首部@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.jpg2、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);


8 0