Masonry使用心得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值