Masonry

一、调用Masonry库的准备

使用pch文件添加全局宏定义(Pre-Compiled Header)
pch文件即 扩展名为.pch的预编译文件。是将工程中较稳定的不会经常修改的代码预先编译好,放在一个公共的文件(.pch)里。

0>将Masonry文件拖入项目中
1>新建pch文件



2>选中项目-->TARGETS-->Build Setting(选中All)-->搜索prefi



3>双击并将pch文件拖入框中
4>设置为Yes

5>填入代码

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

#endif /* PrefixHeader_pch */

二 、使用方法

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;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;

下面是关于这些属性的介绍



2.约束的类型

 1.尺寸:width、height、size
 2.边界:left、leading、right、trailing、top、bottom
 3.中心点:center、centerX、centerY
 4.边界:edges
 5.偏移量:offset、inset、insets、sizeOffset、centerOffset
 6.优先级:priority()约束优先级(0~1000)、priorityLow()\*500*\、priorityMedium()\*500*\、priorityHigh()\*750*\
 7.比例:multipliedBy乘因数、 dividedBy除因数

3.两个区别

1>mas_equalToequalTo:

默认情况下mas_equalTo有自动包装功能,比如自动将20包装为@20equalTo没有自动包装功能。

如果添加了下面的宏,那么mas_equalToequalTo就没有区别:
#define MAS_SHORTHAND_GLOBALS

2>mas_heightheight:

默认情况下widthmake对象的一个属性,用来添加宽度约束用的,表示对宽度进行约束。mas_width是一个属性值,用来当做equalTo的参数,表示某个控件的宽度属性。

如果添加了下面的宏,mas_width也可以写成width:
#define MAS_SHORTHAND

4.基本使用

mas_makeConstraints://添加约束
mas_updateConstraints://更新约束、亦可添加新约束
mas_remakeConstraints://重置之前的约束

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];

5.注意点

使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraints属性为NO;
防止block中的循环引用,使用弱引用(这是错误观点),在这里block是局部的引用,block内部引用self不会造成循环引用的
__weak typeof (self) weakSelf = self;(没必要的写法)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,798评论 0 1
  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,371评论 0 3
  • 安静的夜,听着张悬的《宝贝》,莫名的落下泪来,不知为谁。 太多人每天生活在这繁华闹市,看似欢笑,实则孤独。什么时候...
    _一方天地_阅读 672评论 0 1
  • 书中自有黄金屋,千钟粟,颜如玉。 如何快速阅读专业书籍,短时间内精通多个领域,把最新的信息装进脑汁,多一些闲暇的时...
    王二公子阅读 680评论 5 14
  • 昨晚有幸加入一微课堂体验绘画治疗,晚上8点准时开始,童老师介绍了什么是绘画治疗,作用疗效,和绘画治疗的6个阶段(放...
    Nicole观莲阅读 301评论 0 0