MasonryGitHub上的官方使用教程翻译

Masonry是一个轻量级的布局框架,简化了AutoLayout的使用,提供链式语法描述NSLayoutConstraints。尽管Swift有SnapKit,Masonry仍被用于支持iOS和Mac OS X的项目。本文介绍了Masonry的优势,如DSL布局,减少了代码冗余,同时讲解了如何使用MASConstraintMaker创建约束,设置优先级,以及解决布局问题和调试方法。
摘要由CSDN通过智能技术生成

Masonry仍旧在持续维护中,借助于开放社区,我们致力于修复漏洞和合并优秀的需求。然而你的项目是用Swift语言的,我们推荐使用SnapKit,因为它有着更简短的API来更好适应Swift.
Masonry是一种轻量级的布局框架,采用了更良好的语言来封装AutoLayout。Masonry有自己的布局DSL,提供了一种链式方式来描述你的NSLayoutConstraints,通过这种方式产生的布局代码更简洁,更易读。Masonry支持iOS和Mac OS X.

NSLayoutConstraints的弊端在哪?
通过底层的AutoLayout可以比较有效和灵活的组织和布局你的Views,然而通过代码创建约束比较冗长且不生动。试想这样一个例子:创建一个View距离父视图的边界都为10,代码如下:

UIView *superview = self.view;

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[superview addConstraints:@[

    //view1 constraints
    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeTop
                                multiplier:1.0
                                  constant:padding.top],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeLeft
                                multiplier:1.0
                                  constant:padding.left],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1.0
                                  constant:-padding.bottom],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeRight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeRight
                                multiplier:1
                                  constant:-padding.right],

 ]];

像这种简单的例子代码都如此冗长,当你的视图复杂的时候可读性就更差了。另外一种方式可以使用VFL,代码更少点,不过这种ASCII风格的语言有它自己的弊端并且实现动画效果比较难,就比如NSLayoutConstraint constraintsWithVisualFormat:返回一个数组。
认识下MASConstraintMaker
同样的例子我们使用MASConstraintMaker来编写

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值