首先说一下这篇博客尽管是标记为原创,可是事实并不是本人亲自写出来的。知识点和样例本人花了一天各处查找和整理终于决定写一个汇总的具体解释,解去各位朋友到处盲目查找的必要,由于不是转载某一个人的内容。故此不标记为转载,由于增加了个人的理解和细心整理所以标为原创,请谅解!

1.首先我们要明白,当我们使用自己主动布局的时候为了不让Contraint和view本身的autoresize属性发生冲突,我们首先须要把控件 的属性设为setTranslatesAutoresizingMaskIntoConstraints:NO

2.分清楚两个函数的不同用处

@1 + (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;

@2
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

使用之前先明白一点:既然是布局。再我们布局的时候必须是相对的对吧,所以我们布局通常都是把两个view在一起捆绑。相互之间存在某种关系做一些约束。通常不存在一个view布局的现象!

注意到第一个函数是一个格式化输入( format),外加输出的是一个数组,一般来说格式化的会包括非常多信息量的,所以这个也不例外。能够包括很多布局信息

首先说一下两个函数的使用的不同方式

constraintWithItem函数的使用方式直接用addConstraint加入到父亲视图就好了

eg:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail

                                                          attribute:NSLayoutAttributeHeight

                                                          relatedBy:NSLayoutRelationEqual

                                                             toItem:nil

                                                          attribute:NSLayoutAttributeNotAnAttribute

                                                         multiplier:1.0

                                                           constant:35]];

创建约束的方式例如以下:

[NSLayoutConstraint constraintWithItem:(id)item

                             attribute:(NSLayoutAttribute)attribute

                             relatedBy:(NSLayoutRelation)relation

                                toItem:(id)otherItem

                             attribute:(NSLayoutAttribute)otherAttribute

                            multiplier:(CGFloat)multiplier

                              constant:(CGFloat)constant]

 

 

參数说明:

第一个參数:指定约束左边的视图view1

第二个參数:指定view1的属性attr1。详细属性见文末。

第三个參数:指定左右两边的视图的关系relation,详细关系见文末。

第四个參数:指定约束右边的视图view2

第五个參数:指定view2的属性attr2,详细属性见文末。

第六个參数:指定一个与view2属性相乘的乘数multiplier

第七个參数:指定一个与view2属性相加的浮点数constant

 

这个函数的对比公式为:

view1.attr1 <relation> view2.attr2 * multiplier + constant



注意:

1.假设你想设置的约束里不须要第二个view,要将第四个參数设为nil,第五个參数设为NSLayoutAttributeNotAnAttribute

 

举例:

[NSLayoutConstraint constraintWithItem:view1

                             attribute:NSLayoutAttributeLeft

                             relatedBy:NSLayoutRelationEqual

                                toItem:view2

                             attribute:NSLayoutAttributeRight

                            multiplier:1

                              constant:10]



翻译过来就是:view1的左側,在,view2的右側,再多10个点。的地方(经典)。

 

附视图的属性和关系的值:

typedef NS_ENUM(NSInteger, NSLayoutRelation) {

    NSLayoutRelationLessThanOrEqual = -1,          //小于等于

    NSLayoutRelationEqual = 0,                     //等于

    NSLayoutRelationGreaterThanOrEqual = 1,        //大于等于

};

typedef NS_ENUM(NSInteger, NSLayoutAttribute) {

    NSLayoutAttributeLeft = 1,                     //左側

    NSLayoutAttributeRight,                        //右側

    NSLayoutAttributeTop,                          //上方

    NSLayoutAttributeBottom,                       //下方

    NSLayoutAttributeLeading,                      //首部

    NSLayoutAttributeTrailing,                     //尾部

    NSLayoutAttributeWidth,                        //宽度

    NSLayoutAttributeHeight,                       //高度

    NSLayoutAttributeCenterX,                      //X轴中心

    NSLayoutAttributeCenterY,                      //Y轴中心

    NSLayoutAttributeBaseline,                     //文本底标线

                                                                                                                                                    

    NSLayoutAttributeNotAnAttribute = 0            //没有属性

};

 

NSLayoutAttributeLeft/NSLayoutAttributeRight 和 NSLayoutAttributeLeading/NSLayoutAttributeTrailing的差别是left/right永远是指左右,而leading/trailing在某些从右至左习惯的地区会变成,leading是右边,trailing是左边。(大概是⊙﹏⊙b)

实战解析:

UILabel *note = [[UILabel alloc] init];

    [note setText:@"欢迎提出宝贵意见!

您留下的每一份心意都将浇灌母婴宝的茁壮成长。"];

    [note setLineBreakMode:NSLineBreakByWordWrapping];

    note.numberOfLines = 0;

    [self.view addSubview:note];

    

    //将自适应向布局约束的转化关掉(依据情况有时须要有时不须要)

    [note setTranslatesAutoresizingMaskIntoConstraints:NO];

    

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:note

                                                          attribute:NSLayoutAttributeTop

                                                          relatedBy:NSLayoutRelationEqual

                                                             toItem:_nab //_nab是导航条

                                                          attribute:NSLayoutAttributeBottom

                                                         multiplier:1.0

                                                           constant:10]];

文字解释:这个文字处于导航条以下+10的位置:

代码解释:note.frame.origin.y = _nab.frame.origin.y+_nab.frame.size.height + 10

字面量解释: note.NSLayoutAttributeTop
NSLayoutRelationEqual _nab.NSLayoutAttributeBottom*1.0 + 10

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:note

                                                          attribute:NSLayoutAttributeLeft

                                                          relatedBy:NSLayoutRelationEqual

                                                             toItem:self.view

                                                          attribute:NSLayoutAttributeLeft

                                                         multiplier:1.0

                                                           constant:10]];

这个大家算是给你自己一个測试,看能不能对着讲出它的含义来,注意一点他们的约束都是加在他们共同的父视图self.view上

关于constraintsWithVisualFormat:函数介绍:

constraintsWithVisualFormat:參数为NSString型。指定Contsraint的属性。是垂直方向的限定还是水平方向的限定,參数定义一般例如以下:

V:|-(>=XXX) :表示垂直方向上相对于SuperView大于、等于、小于某个距离

若是要定义水平方向,则将V:改成H:就可以

在接着后面-[]中括号中面对当前的View/控件 的高度/宽度进行设定;

options:字典类型的值;这里的值一般在系统定义的一个enum里面选取

metrics:nil。一般为nil ,參数类型为NSDictionary,从外部传入 //衡量标准

views:就是上面所增加到NSDictionary中的绑定的View

在这里要注意的是 AddConstraints  和 AddConstraint 之间的差别。一个加入的參数是NSArray,一个是NSLayoutConstraint

使用规则

|: 表示父视图

-:表示距离

V:  :表示垂直

H:  :表示水平

>= :表示视图间距、宽度和高度必须大于或等于某个值

<= :表示视图间距、宽度和高度必须小宇或等于某个值

== :表示视图间距、宽度或者高度必须等于某个值

@  :>=、<=、==  限制   最大为  1000

1.|-[view]-|:  视图处在父视图的左右边缘内

2.|-[view]  :   视图处在父视图的左边缘

3.|[view]   :   视图和父视图左边对齐

4.-[view]-  :  设置视图的宽度高度

5.|-30.0-[view]-30.0-|:  表示离父视图 左右间距  30

6.[view(200.0)] : 表示视图宽度为 200.0

7.|-[view(view1)]-[view1]-| :表示视图宽度一样。而且在父视图左右边缘内

8. V:|-[view(50.0)] : 视图高度为  50

9: V:|-(==padding)-[imageView]->=0-[button]-(==padding)-| : 表示离父视图的距离

为Padding,这两个视图间距必须大于或等于0而且距离底部父视图为 padding。

10:  [wideView(>=60@700)]  :视图的宽度为至少为60 不能超过  700

11: 假设没有声明方向默觉得  水平  V:

使用方式:

1.首先须要创建一个数组来保存,这个约束返回的数组

NSMutableArray *tmpConstraints = [NSMutableArray array];

2.第二点就是创建约束,创建的方式按上面的规定写

[tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tfEmail]-10-[btnSubmit(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tfEmail,btnSubmit)]];

[tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[btnSubmit]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnSubmit)]];

3.把创建的约束保存到之前创建的数组中

4.把这个包括约束的总数组,加到他们共同的父视图self.view上作为一个约束数组传递进去

[self.view addConstraints:tmpConstraints];

实战解读:

[tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[btnSubmit]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnSubmit)]];

H 水平方向

:| 父视图左边

- 距离

10 10个像素

- 距离

[btnSubmit] 提交button

- 距离

10 10个像素

- 距离

| 父视图右边

总的解读:这个提交button距离父视图左边10个像素。右边10个像素

[tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tfEmail]-10-[btnSubmit(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tfEmail,btnSubmit)]];

V:垂直方向上

[tfEmail] 邮件输入框

- 距离

10 10个像素

-距离

[btnSubmit(==35)] 提交button height = 35

总的解读:提交button再输入框以下10个位置处

关于绑定元素的理解:我是这样理解的:用到两个元素时,就绑定两个。用到一个,绑定一个即可了

鉴于自己主动布局的复杂性,我将继续研究。更新中。。。

。。

大牛给的资料大集合,超级赠送,噢耶。!赶快收藏吧!

http://commandshift.co.uk/blog/2013/02/20/creating-individual-layout-constraints/



http://blogs.burnsidedigital.com/2014/06/introduction-to-visual-format-language-for-auto-layout-on-ios/

VFL http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/



http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/

autolayout 在项目中的使用

https://github.com/smileyborg/TableViewCellWithAutoLayout  cell

https://github.com/smileyborg/PureLayout  pureLayout

 

http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights



masonry

http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/





自己主动布局中的一些解说(setNeedsUpdateConstraints,updateConstraintsIfNeeded)等

http://my.oschina.net/w11h22j33/blog/208574

最新文章

  1. 异常处理_Maven之web项目java.lang.LinkageError
  2. akka实现的actor
  3. 移动端的拖拽这个demo实现的功能
  4. 一个简单的零配置命令行HTTP服务器 - http-server (nodeJs)
  5. Web设计者和开发者必备的28个Chrome插件
  6. python中的协程及实现
  7. 易酷 cms2.5 本地文件包含漏洞 getshell
  8. PMP证书的获取,不知道10大注意事项会吃亏
  9. C++11 中的function和bind、lambda用法
  10. etcd-v2第一集
  11. 异常 java.net.ConnectException: Connection refused: no further information
  12. 【转】性能测试,影响 TPS 的一些因素
  13. solr学习四(关于性能的杂知识)
  14. Jump Game leetcode java
  15. unity physics joint
  16. 使用DroneKit控制无人机
  17. Mysql6.0连接中的几个问题 Mysql6.xx
  18. my29_PXC集群状态查看
  19. mongo简介
  20. OVS编译

热门文章

  1. 2014 百度之星 1003 题解 Xor Sum
  2. Git-如何将已存在的项目提交到git
  3. windows电脑空间清理
  4. Android开发日志统一管理
  5. tensorflow 问题库
  6. JAVA程序类加载及其反射机制
  7. vue中的三级联动
  8. iotop---监控磁盘I/O 使用状况
  9. Maven 工程 POM.XML文件最全详解
  10. IDEA修改当前工程jdk版本