Masonry是目前最流行的AutoLayout框架.
-
使用:将Masonry文件包拖入项目,删掉自带的info.plist文件.demo中对Masonry的引用在.pch文件中.
-
使用Masonry不需要设置
控件的translatesAutoresizingMaskIntoConstraints属性为NO;
Masonry简单实现
(左下角正方形):
[targetView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(-20);
make.leading.equalTo(self.view).offset(20);
make.size.mas_equalTo(CGSizeMake(100, 100));
}];
内边距为50:
- 第一种实现方式
make.leading.top.mas_equalTo(@50); make.trailing.bottom.mas_equalTo(@-50);
- 第二种实现方式
make.edges.insets(UIEdgeInsetsMake(50, 50, 50, 50));
Masonry使用技巧:
定义以下两个宏,在使用Masonry框架时就不需要加mas_前缀了
(定义宏一定要在引入Masonry.h文件之前).
//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
使用Masonry添加兄弟控件约束不需要考虑父控件.
具体实现代码如下:
[leftView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.offset(20);
make.bottom.offset(-20);
make.trailing.equalTo(rightiew.leading).offset(-20);
make.height.equalTo(100);
make.height.equalTo(rightiew.height);
}];
[rightiew makeConstraints:^(MASConstraintMaker *make) {
make.trailing.equalTo(self.view.trailing).offset(-20);
make.top.equalTo(leftView.top);
make.width.equalTo(leftView.width);
}];
原文链接:http://www.jianshu.com/p/d172131d24c9