Masonry自动布局使用
2015-12-18 00:06
239 查看
Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL。简洁明了并具有高可读性而且同时支持iOS和MaxOSX。
下载
NSLayoutConstraints的缺点
NSLayoutConstraints是一个强大且灵活的自动布局架构,可是通过代码创建的约束是十分冗余,下面我们通过一段代码来实现你想要一个视图铺满它的父视图。但是边距为10
即使一个简单的例子所需的代码都相当冗长,当你有超过2或3视图时变得不可读,另一种选择是使用可视化格式语言(VFL),有点不太冗长。然而,ASCII类型语法上有它自己的陷阱并且作为NSLayoutConstraintconstraintsWithVisualFormat:添加动画效果返回一个数组也有点难。
Masonry的优点
下面是使用MASConstraintMaker创建同样的约束
UIEdgeInsetspadding=UIEdgeInsetsMake(10,10,10,10);
[self.subviewmas_makeConstraints:^(MASConstraintMaker*make){
make.top.equalTo(self.view.mas_top).with.offset(padding.top);
make.left.equalTo(self.view.mas_left).with.offset(padding.left);
make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
}];
更短代码实现
此外还需注意使用NSLayoutConstraints时调用了[superviewaddConstraints:...]方法,在Masonry库中是自动向当前视图添加约束的,我们也可以通过self.subview.translatesAutoresizingMaskIntoConstraints=NO来手动设置。
并不是所有创建都一样
.equalTo 等价于NSLayoutRelationEqual
.lessThanOrEqualTo等价于NSLayoutRelationLessThanOrEqual
.greaterThanOrEqualTo等价于NSLayoutRelationGreaterThanOrEqual
这些三个等式约束可以是下列任一操作作为一个参数
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
然而自动布局不允许对齐属性如左对齐、右对齐,centerY等将设置为常量值
您可以使用基元和结构打造你的约束替代NSNumber。默认情况下,支持自动装箱的宏均以mas_作为前缀。没有前缀的版本均可通过导入之前定义MAS_SHORTHAND_GLOBALS。
优先原则
.priority允许你指定优先级
.priorityHigh等价于UILayoutPriorityDefaultHigh高优先级
.priorityMedium中等优先级
.priorityLow等价于UILayoutPriorityDefaultLow
组成
Masonry也提供了几个方便的方法,同时创建多个约束,被称为MASCompositeConstraints。
NSLayoutConstraints的缺点
NSLayoutConstraints是一个强大且灵活的自动布局架构,可是通过代码创建的约束是十分冗余,下面我们通过一段代码来实现你想要一个视图铺满它的父视图。但是边距为10
UIView*superview=self; UIView*view1=[[UIViewalloc]init]; view1.translatesAutoresizingMaskIntoConstraints=NO; view1.backgroundColor=[UIColorgreenColor]; [superviewaddSubview:view1]; UIEdgeInsetspadding=UIEdgeInsetsMake(10,10,10,10); [superviewaddConstraints:@[ //view1constraints [NSLayoutConstraintconstraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraintconstraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraintconstraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraintconstraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]];
即使一个简单的例子所需的代码都相当冗长,当你有超过2或3视图时变得不可读,另一种选择是使用可视化格式语言(VFL),有点不太冗长。然而,ASCII类型语法上有它自己的陷阱并且作为NSLayoutConstraintconstraintsWithVisualFormat:添加动画效果返回一个数组也有点难。
Masonry的优点
下面是使用MASConstraintMaker创建同样的约束
UIEdgeInsetspadding=UIEdgeInsetsMake(10,10,10,10);
[self.subviewmas_makeConstraints:^(MASConstraintMaker*make){
make.top.equalTo(self.view.mas_top).with.offset(padding.top);
make.left.equalTo(self.view.mas_left).with.offset(padding.left);
make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
}];
更短代码实现
[self.subviewmas_makeConstraints:^(MASConstraintMaker*make){ make.edges.equalTo(self.view).with.insets(padding); }];
此外还需注意使用NSLayoutConstraints时调用了[superviewaddConstraints:...]方法,在Masonry库中是自动向当前视图添加约束的,我们也可以通过self.subview.translatesAutoresizingMaskIntoConstraints=NO来手动设置。
并不是所有创建都一样
.equalTo 等价于NSLayoutRelationEqual
.lessThanOrEqualTo等价于NSLayoutRelationLessThanOrEqual
.greaterThanOrEqualTo等价于NSLayoutRelationGreaterThanOrEqual
这些三个等式约束可以是下列任一操作作为一个参数
1.MASViewAttribute
MASViewAttribute | NSLayoutAttribute |
---|---|
view.mas_left | NSLayoutAttributeLeft |
view.mas_right | NSLayoutAttributeRight |
view.mas_top | NSLayoutAttributeTop |
view.mas_bottom | NSLayoutAttributeBottom |
view.mas_leading | NSLayoutAttributeLeading |
view.mas_trailing | NSLayoutAttributeTrailing |
view.mas_width | NSLayoutAttributeWidth |
view.mas_height | NSLayoutAttributeHeight |
view.mas_centerX | NSLayoutAttributeCenterX |
view.mas_centerY | NSLayoutAttributeCenterY |
view.mas_baseline | NSLayoutAttributeBaseline |
2.UIView/NSView
如果你想要view.left大于或等于label.left可以make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
3.NSNumber
自动布局允许宽度和高度设置为常量值。如果你想要将视图具有最小值和最大宽度设置你可以//width>=200&&width<=400
make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400)
然而自动布局不允许对齐属性如左对齐、右对齐,centerY等将设置为常量值
//createsview.left=view.superview.left+10
make.left.lessThanOrEqualTo(@10)
make.top.mas_equalTo(42);
make.height.mas_equalTo(20);
make.size.mas_equalTo(CGSizeMake(50,100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10,0,10,0));
make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10,0,10,0));
您可以使用基元和结构打造你的约束替代NSNumber。默认情况下,支持自动装箱的宏均以mas_作为前缀。没有前缀的版本均可通过导入之前定义MAS_SHORTHAND_GLOBALS。
4.NSArray
make.height.equalTo(@[view1.mas_height,view2.mas_height]);
make.height.equalTo(@[view1,view2]);
make.left.equalTo(@[view1,@100,view3.right]);
优先原则
.priority允许你指定优先级
.priorityHigh等价于UILayoutPriorityDefaultHigh高优先级
.priorityMedium中等优先级
.priorityLow等价于UILayoutPriorityDefaultLow
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow(); make.top.equalTo(label.mas_top).with.priority(600);
组成
Masonry也提供了几个方便的方法,同时创建多个约束,被称为MASCompositeConstraints。
edges
//maketop,left,bottom,rightequalview2
make.edges.equalTo(view2);
//maketop=superview.top+5,left=superview.left+10,
//bottom=superview.bottom-15,right=superview.right-20
make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5,10,15,20))
center
//makecenterXandcenterY=button1
make.center.equalTo(button1)
//makecenterX=superview.centerX-5,centerY=superview.centerY+10
make.center.equalTo(superview).centerOffset(CGPointMake(-5,10))
//Alledgesbutthetopshouldequalthoseofthesuperview
make.left.right.and.bottom.equalTo(superview);
make.top.equalTo(otherView);
相关文章推荐
- 设备模型的基础---kobject,kset
- 蘑菇街 私有云Docker实例应用
- 使用Python编写简单的端口扫描器的实例分享
- PHP封装的字符串加密解密函数
- 详解 PHP加密解密字符串函数附源码下载
- 解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法
- WordPress中用于获取文章信息以及分类链接的函数用法
- 详解WordPress中用于合成数组的wp_parse_args()函数
- WordPress中用于更新伪静态规则的PHP代码实例讲解
- 详解WordPress中给链接添加查询字符串的方法
- 讲解WordPress开发中一些常用的debug技巧
- WordPress中注册菜单与调用菜单的方法详解
- thinkphp实现163、QQ邮箱收发邮件的方法
- 基于ThinkPHP实现批量删除
- 详解WordPress中过滤链接与过滤SQL语句的方法
- jQuery定义插件的方法
- jQuery Validate表单验证入门学习
- 原生js实现移动端瀑布流式代码示例
- JavaScript jQuery 中定义数组与操作及jquery数组操作
- JavaScript计划任务后台运行的方法