系统自动布局NSLayoutConstraint 学习笔记
目前为止,实现自动布局技术选型方面也可以使用xib和storyboard。在开发过程中通常登录、注册等变动可能性较小的视图,可以采用xib开发,其他页面通常会采用Masonry布局。
使用NSLayoutConstraint之前,我们需要确定一点:为了防止constraint和view本身的autoresizing属性冲突,我们需要设置view的属性(关闭系统的自定义布局):
view.translatesAutoresizingMaskIntoConstraints = NO;
自动布局方法:
[NSLayoutConstraint constraintWithItem:(id)item
attribute:(NSLayoutAttribute)attribute
relatedBy:(NSLayoutRelation)relation
toItem:(id)otherItem
attribute:(NSLayoutAttribute)otherAttribute
multiplier:(CGFloat)multiplier
constant:(CGFloat)constant]
1.参数说明:
第一个参数:指定需要添加约束的视图一view1
第二个参数:指定view1需要添加约束的属性attr1
第三个参数:指定左右两边的视图的关系relation
第四个参数:指定需要添加约束的视图二view2
第五个参数:指定view2需要添加约束的属性attr2
第六个参数:指定一个与view2属性相乘的乘数multiplier
第七个参数:指定一个与view2属性相加的浮点数constant
依据的公式是:view1.attr1 = view2.attr2*multiplier +constant
2.NSLayoutAttribute:约束属性
NSLayoutAttributeLeft 视图的左边;
NSLayoutAttributeRight 视图的右边;
NSLayoutAttributeTop 视图的上边;
NSLayoutAttributeBottom 视图的下边;
NSLayoutAttributeLeading 视图的前边;
NSLayoutAttributeTrailing 视图的后边;
NSLayoutAttributeWidth 视图的宽度;
NSLayoutAttributeHeight 视图的高度;
NSLayoutAttributeCenterX 视图的中点的X值;
NSLayoutAttributeCenterY 视图中点的Y值;
NSLayoutAttributeBaseline 视图的基准线;
NSLayoutAttributeNotAnAttribute 无属性;
3.NSLayoutRelation:约束关系
NSLayoutRelationLessThanOrEqual <=
NSLayoutRelationEqual ==
NSLayoutRelationGreaterThanOrEqual >=
注意点:
①view1可以和view2是一样的,比如我我们可以设置view1的宽度是高度的两倍
②使用NSLayoutAttributeNotAnAttribute时,view2一般为空
自动布局可视化语言VFL:
[NSLayoutConstraint constraintsWithVisualFormat:(NSString *)vflString
options:(NSLayoutFormatOptions)option
metrics:(nullable NSDictionary<NSString *,id> ) metricDic
views:(NSDictionary<NSString>)]viewDic
参数说明:
第一个参数:VFL格式化的字符串;
第二个参数:对齐方式,是个枚举值;
第三个参数:一个字典,key值为VFL语句中的变量,value是对应的数值。
第四个参数:VFL语句中使用到的视图;
2.VFL语法规则:
example:"H:|-space-[leftView(==rightView)]-space1-[rightView]-space-|";
①H表示水平方向,V表示垂直方向;
② |表示superview的边界;
③[]表示view、()表示尺寸,两者可以组合,中间用逗号隔开;
④ -表示间隙;
⑤@表示优先级;
整个句子的解释为:从水平方向superview的左边界开始,先有一个宽度为space的空隙,它的右边是leftView(leftView和rightView一样宽),接着是一个宽度为space1的空隙,接着是rightView,rightView右边是一个宽度为space的空隙。
3.对齐方式:
NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),//左边缘对齐
NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),//右边缘对齐
NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),//顶部边缘对齐
NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),//底部边缘对齐
NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),//前边缘对齐
NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),//后边缘对齐
NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),//垂直方向中心
NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),//水平方向中心
4.view参数中一般会使用NSDictionaryOfVariableBindings方法创建字典;
NSDictionary *views = NSDictionaryOfVariableBindings(leftView,rightView,bottomView);
NSDictionaryOfVariableBindings这个宏可以生成一个变量名到变量值映射的Dictionary。比如NSDictionaryOfVariableBindings(button1, button2)将会生成一个{ @”button1” = button1, @”button2 = button2 }的Dictionary。
注意点:
- VFL语句中具体浮点型不可以使用 56.f 诸如此类,会crash;
- VFL语句中的视图,不要使用self.xxx属性,而要使用变量_xxx,会crash
- addConstraint之前,必须保证subView已拥有superView
- 视图的 translatesAutoresizingMaskIntoConstraints 必须设置为NO,