iOS-iOS下的Masonry适配

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.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);

 

 

 

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值