您的位置:首页 > 其它

Masonry自动布局使用

2015-12-18 00:06 239 查看
  Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL。简洁明了并具有高可读性而且同时支持iOS和MaxOSX。

  下载

  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

MASViewAttributeNSLayoutAttribute
view.mas_leftNSLayoutAttributeLeft
view.mas_rightNSLayoutAttributeRight
view.mas_topNSLayoutAttributeTop
view.mas_bottomNSLayoutAttributeBottom
view.mas_leadingNSLayoutAttributeLeading
view.mas_trailingNSLayoutAttributeTrailing
view.mas_widthNSLayoutAttributeWidth
view.mas_heightNSLayoutAttributeHeight
view.mas_centerXNSLayoutAttributeCenterX
view.mas_centerYNSLayoutAttributeCenterY
view.mas_baselineNSLayoutAttributeBaseline



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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: