IOS AutoLayout详解(三)用代码实现(附Demo下载)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的IOS SDK专栏,这个专栏我会持续进行更新。 IOS SDK详解

前言:

在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建就派上用场了,这篇文章我会详解用代码实现的两个主要函数,然后讲解一个Demo,最后Demo我会附上下载链接。

用代码实现的函数一

第一个函数通过描述两个view的参考线之间的约束来创建约束,例如有一个label和一个textfield。约束这样描述

label的右边参考线和textfield的右边参考线距离恒定为10

函数

+ (

参数的意义:

参数 意义

view1 左手边的受约束视图

attr1 左手边的受约束视图的参考参数

relation 约束的关系

view2 右手边的受约束视图

multiplier The constant multiplied with the attribute on the right-hand side of the constraint as part of getting the modified attribute.

attr2 The constant added to the multiplied attribute value on the right-hand side of the constraint to yield the final modified attribute.

通常,multiplier的值为1.0。这个不太好翻译,我举个例子就懂了 举个例子 如果,我想要一个View的宽度为另一个View的一半,则

[NSLayoutConstraintconstraintWithItem:self.view1attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:self.view2attribute:NSLayoutAttributeWidthmultiplier:0.5constant:0.0]];

这里有个计算公式

attribute1 == multiplier × attribute2 + constant 也就是说,在这里 view1.width = view2.width * 0.5 + 0.0

这样,更能够理解上述函数中两个参数的含义了吧。 再举个例子: 我想让一个View距离右上角(30,30)并且保持自己的长宽不变。实现代码

NSLayoutConstraint * h_c = [NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:self.testviewattribute:NSLayoutAttributeRightmultiplier:1.0constant:30];NSLayoutConstraint * v_c = [NSLayoutConstraint constraintWithItem:self.testviewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeTopmultiplier:1.0constant:30];NSLayoutConstraint * e_w = [NSLayoutConstraint constraintWithItem:self.testviewattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeWidthmultiplier:1.0 constant:CGRectGetWidth(self.testview.frame)];NSLayoutConstraint * e_h = [NSLayoutConstraint constraintWithItem:self.testviewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeHeightmultiplier:1.0 constant:CGRectGetHeight(self.testview.frame)];[self.view addConstraints:@[h_c,v_c,e_h,e_w]];

效果如图

用代码实现的方法二

方法二使用可视化语言VFL 可视化语言的Apple文档链接如下 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html 利用到的函数为

+ (*)*)views

参数的的意义

参数 意义

format NSString类型的可视语言描述

opts 描述可视化语言中对象的layout方向

metrics 描述可视化语言中String代表的常量值,字典类型,key为String,value为NSNumber类型

views 描述可视化语言中String代表的对象,字典类型,key为String,value为layout约束的对象

举个例子就懂了,例如,惰性初始化下面一个View,,不难看出,这个View我没有指定大小,大小我要用约束来创建

-(UIView *)testview{if (!_testview) {_testview = [[UIView alloc] init];_testview.backgroundColor = [UIColor blueColor];}return _testview;}- (void)viewDidLoad {[super viewDidLoad];[];}

然后,我用约束的方式,让View的大小恒定为100*100

NSArray *c_v = [NSLayoutConstraint constraintsWithVisualFormat:@”V:[testview(==100)]”options:0metrics:nilviews:@{@”testview”:self.testview}];NSArray *c_h = [NSLayoutConstraint constraintsWithVisualFormat:@”H:[testview(==100)]”options:0metrics:nilviews:@{@”testview”:self.testview}];[self.view addConstraints:c_h];[self.view addConstraints:c_v];

然后,我再把View约束到距离右上角(30*30)的位置

NSArray *l_v = [NSLayoutConstraint constraintsWithVisualFormat:@”V:|-hdistance-[testview]”options:0metrics:@{@”hdistance”:@(30)}views:@{@”testview”:self.testview}];NSArray *l_h = [NSLayoutConstraint constraintsWithVisualFormat:@”H:[testview]-vdistance-|”options:0metrics:@{@”vdistance”:@(30)}views:@{@”testview”:self.testview}];向上攀爬的。

IOS AutoLayout详解(三)用代码实现(附Demo下载)

相关文章:

你感兴趣的文章:

标签云: