Masonry的使用

 

今天来介绍下Masonry 参考(http://www.cocoachina.com/ios/20141219/10702.html)

Masonry 源码:https://github.com/Masonry/Masonry

Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS Max OS X

podfile 的引用方式

 

platform :ios, '7.0'

 

target ‘MyApp’ do

 

pod 'Masonry'

end

 

看一下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; 文本挤线

 

 

Masonry中能够添加autolayout约束有三个函数

1

2

3

4

5

6

7

8

9

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

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

/*

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

mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况

mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束

三种函数善加利用 就可以应对各种情况了

*/

 

 equalTo mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO

1

2

3

4

#define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))

#define mas_greaterThanOrEqualTo(...)    greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

#define mas_lessThanOrEqualTo(...)       lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

#define mas_offset(...)                  valueOffset(MASBoxValue((__VA_ARGS__)))

可以看到 mas_equalTo只是对其参数进行了一个BOX操作(装箱) MASBoxValue

Masonry的用法:

[view1.mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self).offset(20);

            make.centerX.equalTo(self);

            make.size.mas_equalTo(CGSizeMake(XCFScreenWidth-XCFRecipeCellMarginTitle*2, 50));

        }];

 

[addListButton mas_makeConstraints:^(MASConstraintMaker *make) {

                make.top.equalTo(footer).offset(20);

                make.centerX.equalTo(footer);

                make.size.mas_equalTo(CGSizeMake(120, 40));

            }];

(因为iOS中原点在左上角所以注意使用offset时注意rightbottom用负数)

offset偏移量

 

 

转载于:https://www.cnblogs.com/louisQian/p/5719891.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值