Masonry介绍与使用

Masonry介绍与使用
一、Masonry 概述
Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性,而且同时支持 iOS 和 Max OS X。Masonry是一个用代码写iOS或OS界面的库,可以代替Autolayout。
Masonry的github地址: https://github.com/SnapKit/Masonry
二、Masonry 配置
步骤1.将下载好的三方工程文件内Masonry文件夹添加到自己的工程当中。
步骤2.引入头文件 #import "Masonry.h"
三、 Masonry 使用讲解
1.属性:
序号
属性
说明
1
@property (nonatomic, strong, readonly) MASConstraint *left;
左侧
2
@property (nonatomic, strong, readonly) MASConstraint *top;
上侧
3
@property (nonatomic, strong, readonly) MASConstraint *right;
右侧
4
@property (nonatomic, strong, readonly) MASConstraint *bottom;
下侧
5
@property (nonatomic, strong, readonly) MASConstraint *leading;
首部
6
@property (nonatomic, strong, readonly) MASConstraint *trailing;
尾部
7
@property (nonatomic, strong, readonly) MASConstraint *width;
8
@property (nonatomic, strong, readonly) MASConstraint *height;
9
@property (nonatomic, strong, readonly) MASConstraint *centerX;
横向中点
10
@property (nonatomic, strong, readonly) MASConstraint *centerY;
纵向中点
11
@property (nonatomic, strong, readonly) MASConstraint *baseline;
文本基线
 
这些属性与NSLayoutAttrubute的对照表如下
 
Masonry
NSAutoLayout
说明
left
NSLayoutAttributeLeft
左侧
top
NSLayoutAttributeTop
上侧
right
NSLayoutAttributeRight
右侧
bottom
NSLayoutAttributeBottom
下侧
leading
NSLayoutAttributeLeading
首部
trailing
NSLayoutAttributeTrailing
尾部
width
NSLayoutAttributeWidth
height
NSLayoutAttributeHeight
centerX
NSLayoutAttributeCenterX
横向中点
centerY
NSLayoutAttributeCenterY
纵向中点
baseline
NSLayoutAttributeBaseline
文本基线
2 方法
     - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
     - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
     - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
   
     mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错
       约束有几种,分别是边距,宽,高,左上右下距离,基准线。添加过约束后可以有修正,修正有offset(位移)修正和multipliedBy(倍率)修正。
 
     mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
     mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束
    
     三种函数善加利用 就可以应对各种情况了
 
3示例
[view mas_remakeConstraints:^(MASConstraintMaker *make) {
// 添加居中约束     
make.center.equalTo(self.view);
// 添加大小约束      
make.size.mas_equalTo(CGSizeMake(200, 300)); 
}];
 
四、注意事项
注意点1: 使用mas_makeConstraints方法的元素必须事先添加到父元素的中,例如[self.view addSubview:view];
    
注意点2: masequalTo 和 equalTo 区别:masequalTo 比equalTo多了类型转换操作,一般来说,大多数时候两个方法都是 通用的,但是对于数值元素使用mas_equalTo。对于对象或是多个属性的处理,使用equalTo。特别是多个属性时,必须使用equalTo,例如 make.left.and.right.equalTo(self.view);
    
注意点3: 注意到方法with和and,这连个方法其实没有做任何操作,方法只是返回对象本身,这这个方法的左右完全是为了方法写的时候的可读性 。make.left.and.right.equalTo(self.view);和make.left.right.equalTo(self.view);是完全一样的,但是明显的加了and方法的语句可读性 更好点。

转载于:https://www.cnblogs.com/lh1204/p/5067016.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值