首页 > 其他 > 详细

Masonry的使用

时间:2016-07-30 00:18:04      阅读:199      评论:0      收藏:0      [点我收藏+]

 

今天来介绍下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偏移量

 

 

Masonry的使用

原文:http://www.cnblogs.com/louisQian/p/5719891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!