在XCode5中,如果我们添加一个Button或者Label,或者其他的什么标准View,而不设置任何constraints,IB会自动生成constraints,而这些constraints是fixed,无法根据intrinsic content size的变化而变化,这并不是我们想要的。比如,我在View中添加一个label, label的title是Label,另加一个Button,点击Button后改变label的title为很长的文字,并且调用invalidateInstrinsicContentSize,在执行前后,分别打印constraints,代码如下:

  1. NSLog(@"current constraints %@ ", [[self textLabel] constraintsAffectingLayoutForAxis:UILayoutConstraintAxisHorizontal]);
  2. NSLog(@"current constraints %@ ", [[self textLabel] constraintsAffectingLayoutForAxis:UILayoutConstraintAxisVertical]);
  3.  
  4. [[self textLabel] setText:@"weuituwjaskdtuqwieotuasdjgakleutaiosgajksdgequwtioasdjgkasjdgkajskgd"];
  5. [[self textLabel] invalidateIntrinsicContentSize];
  6. NSLog(@"current constraints %@ ", [[self textLabel] constraintsAffectingLayoutForAxis:UILayoutConstraintAxisHorizontal]);
  7. NSLog(@"current constraints %@ ", [[self textLabel] constraintsAffectingLayoutForAxis:UILayoutConstraintAxisVertical]);

这时看到的输出是

  1. 2014-07-04 17:46:28.580 AutoLayoutExample[1110:70b] current constraints (
  2. "<NSIBPrototypingLayoutConstraint:0x8b95fb0 'IB auto generated at build time for view with fixed frame' H:|-(179)-[UILabel:0x8b93e80](LTR) (Names: '|':UIView:0x8b95290 )>",
  3. "<NSIBPrototypingLayoutConstraint:0x8b960e0 'IB auto generated at build time for view with fixed frame' H:[UILabel:0x8b93e80(42)]>"
  4. )
  5. 2014-07-04 17:46:28.582 AutoLayoutExample[1110:70b] current constraints (
  6. "<NSIBPrototypingLayoutConstraint:0x8b95d60 'IB auto generated at build time for view with fixed frame' V:|-(147)-[UILabel:0x8b93e80] (Names: '|':UIView:0x8b95290 )>",
  7. "<NSIBPrototypingLayoutConstraint:0x8b96110 'IB auto generated at build time for view with fixed frame' V:[UILabel:0x8b93e80(21)]>"
  8. )
  9.  
  10. 2014-07-04 17:46:28.586 AutoLayoutExample[1110:70b] current constraints (
  11. "<NSIBPrototypingLayoutConstraint:0x8b95fb0 'IB auto generated at build time for view with fixed frame' H:|-(179)-[UILabel:0x8b93e80](LTR) (Names: '|':UIView:0x8b95290 )>",
  12. "<NSIBPrototypingLayoutConstraint:0x8b960e0 'IB auto generated at build time for view with fixed frame' H:[UILabel:0x8b93e80(42)]>"
  13. )
  14. 2014-07-04 17:46:28.587 AutoLayoutExample[1110:70b] current constraints (
  15. "<NSIBPrototypingLayoutConstraint:0x8b95d60 'IB auto generated at build time for view with fixed frame' V:|-(147)-[UILabel:0x8b93e80] (Names: '|':UIView:0x8b95290 )>",
  16. "<NSIBPrototypingLayoutConstraint:0x8b96110 'IB auto generated at build time for view with fixed frame' V:[UILabel:0x8b93e80(21)]>"
  17. )

可以看出IB产生的是IB auto generated at build time for view with fixed frame,无论intrinsic content size如何变化,都无济于事。所以,在使用AutoLayout时,切记:每个元素都应该有能完全确定size和位置的constraints, 如果有一样不能确定,IB都会提示Error 或者Warning,而这些Error或者Warning也是必须解决的。

关于两种PlaceHolder

placeholder constraits 和placeholder instrinsic content size

placeholder constraits是说这个constraint只是个placeholder,在build时就会被去掉,这样可以阻止IB自动生成constraints;placeholder instrinsic content size是說在IB中设计时使用的size,在runtime时则需要调用intrinsicContentSize方法来确定。

下面说说intrinsicContentSize和constraint的关系:intrinsicContentSize和constraint是Auto Layout的支柱概念,缺一不可。同时有时intrinsicContentSize可以在某个方面也是一种constraint,通常unambiguous layout generally requires setting two attributes in each axis(也就是在一个维度上需要设置起点和长度两个信息). When a view has an intrinsic content size, that size accounts for one of the two attributes.(如果一个view 有intrinsicContentSize,并且设计者想这样,intrinsicContentSize是可以起到那个长度的限制作用的) You can, for example, place a text-based control or an image view in the center of its superview, and its layout will not be ambiguous. The intrinsic content size plus the location combine for a fully specified placement. 能够这样的前提是1. 使用系统的标准View,2. 在IB中指定Intrinsic size时需要用Default(System defined),比如如果只指定位置信息,不指定size,使用system default intrinsic size,时,输出时这样的

  1. 2014-07-04 22:25:40.866 AutoLayoutExample[2237:70b] current constraints (
  2. "<NSLayoutConstraint:0x8a6bde0 H:|-(179)-[UILabel:0x8a6a010] (Names: '|':UIView:0x8a6b400 )>",
  3. "<NSContentSizeLayoutConstraint:0x8b847a0 H:[UILabel:0x8a6a010(42)] Hug:251 CompressionResistance:750>"
  4. )
  5. 2014-07-04 22:25:40.868 AutoLayoutExample[2237:70b] current constraints (
  6. "<NSLayoutConstraint:0x8a6bd30 V:[_UILayoutGuide:0x8a6b4b0]-(127)-[UILabel:0x8a6a010]>",
  7. "<_UILayoutSupportConstraint:0x8a67660 V:[_UILayoutGuide:0x8a6b4b0(20)]>",
  8. "<_UILayoutSupportConstraint:0x8a6a300 V:|-(0)-[_UILayoutGuide:0x8a6b4b0] (Names: '|':UIView:0x8a6b400 )>",
  9. "<NSContentSizeLayoutConstraint:0x8b847e0 V:[UILabel:0x8a6a010(21)] Hug:251 CompressionResistance:750>"
  10. )
  11. 2014-07-04 22:25:40.869 AutoLayoutExample[2237:70b] current constraints (
  12. "<NSLayoutConstraint:0x8a6bde0 H:|-(179)-[UILabel:0x8a6a010] (Names: '|':UIView:0x8a6b400 )>",
  13. "<NSContentSizeLayoutConstraint:0x8b847a0 H:[UILabel:0x8a6a010(42)] Hug:251 CompressionResistance:750>"
  14. )
  15. 2014-07-04 22:25:40.870 AutoLayoutExample[2237:70b] current constraints (
  16. "<NSLayoutConstraint:0x8a6bd30 V:[_UILayoutGuide:0x8a6b4b0]-(127)-[UILabel:0x8a6a010]>",
  17. "<_UILayoutSupportConstraint:0x8a67660 V:[_UILayoutGuide:0x8a6b4b0(20)]>",
  18. "<_UILayoutSupportConstraint:0x8a6a300 V:|-(0)-[_UILayoutGuide:0x8a6b4b0] (Names: '|':UIView:0x8a6b400 )>",
  19. "<NSContentSizeLayoutConstraint:0x8b847e0 V:[UILabel:0x8a6a010(21)] Hug:251 CompressionResistance:750>"
  20. )

如果只指定位置信息,但不指定size信息,但intrisic size是PlaceHolder,width 和height 都是None时输出是这样的:

  1. 2014-07-04 21:11:56.941 AutoLayoutExample[1790:70b] current constraints (
  2. "<NSLayoutConstraint:0x8ad9670 H:|-(179)-[UILabel:0x8ad7880] (Names: '|':UIView:0x8ad8c90 )>",
  3. "<NSIBPrototypingLayoutConstraint:0x8ad9c10 'IB auto generated at build time for view with ambiguity' H:[UILabel:0x8ad7880(42@251)] priority:251>"
  4. )
  5. 2014-07-04 21:11:56.942 AutoLayoutExample[1790:70b] current constraints (
  6. "<NSLayoutConstraint:0x8ad95c0 V:[_UILayoutGuide:0x8ad8d40]-(127)-[UILabel:0x8ad7880]>",
  7. "<_UILayoutSupportConstraint:0x8a6d3f0 V:[_UILayoutGuide:0x8ad8d40(20)]>",
  8. "<_UILayoutSupportConstraint:0x8aaf250 V:|-(0)-[_UILayoutGuide:0x8ad8d40] (Names: '|':UIView:0x8ad8c90 )>",
  9. "<NSIBPrototypingLayoutConstraint:0x8ad9c40 'IB auto generated at build time for view with ambiguity' V:[UILabel:0x8ad7880(21@251)] priority:251>"
  10. )
  11. 2014-07-04 21:11:56.943 AutoLayoutExample[1790:70b] current constraints (
  12. "<NSLayoutConstraint:0x8ad9670 H:|-(179)-[UILabel:0x8ad7880] (Names: '|':UIView:0x8ad8c90 )>",
  13. "<NSIBPrototypingLayoutConstraint:0x8ad9c10 'IB auto generated at build time for view with ambiguity' H:[UILabel:0x8ad7880(42@251)] priority:251>"
  14. )
  15. 2014-07-04 21:11:56.944 AutoLayoutExample[1790:70b] current constraints (
  16. "<NSLayoutConstraint:0x8ad95c0 V:[_UILayoutGuide:0x8ad8d40]-(127)-[UILabel:0x8ad7880]>",
  17. "<_UILayoutSupportConstraint:0x8a6d3f0 V:[_UILayoutGuide:0x8ad8d40(20)]>",
  18. "<_UILayoutSupportConstraint:0x8aaf250 V:|-(0)-[_UILayoutGuide:0x8ad8d40] (Names: '|':UIView:0x8ad8c90 )>",
  19. "<NSIBPrototypingLayoutConstraint:0x8ad9c40 'IB auto generated at build time for view with ambiguity' V:[UILabel:0x8ad7880(21@251)] priority:251>"
  20. )

size信息部分是由IB自动生成的,但是它们的priority只有251,而不是之前的1000(required)了。

只指定位置,对比Default 和 PlaceHolder的 constraints,可以看到,在使用Default时,IB给自动加了NSContentSizeLayoutConstraint;在使用Placeholder时,IB自动生成了NSIBPrototypingLayoutConstraint,因为我们使用了PlaceHolder,并且size都是0,所以IB已经无法知道size信息了,为了避免ambiguity就只能自动加了。

我们在使用custom view时,需要指定intrinsic size为placeholder,那我们应该怎么解决IB自动添加ambiguity的constraint的问题呢?

事实上,如果我们的view是个custom view的话,IB是不会自动添加ambity的constraint,IB会添加NSContentSizeLayoutConstraint,然后在runtime时调用custom view 的intrinsicContentSize来知道view应该有多大。

还有继续说说intrinsicContentSize,事实上它不是一个人在战斗,其他的constraint只有一个priority,但是intrinsicContentSize有两个,分别叫Content Hugging Priority和Content Compression Resistancy Priority。 Content Hugging Priority控制当view frame>intrinsicContentSize时,是不是要缩小view 的size;而Content Compression Resistancy Priority控制当view frame<intrinsicContentSize时, 要不要截断content,保持view frame size。

举例来说,如果我有个UILabel,开始Title为“Label”,设置Width constraint 为42@500,设置Content Hugging Priority为1000, 设置Content Compression Resistancy Priority为1。如果我在runtime时改动Title为“S”,那整个UILabel的size都被缩小到只能容下5,因为这时frame size大于IntrinsicContentSize,Content Hugging Priority为1000,要比width constraint高,所以应该是Content Hugging 起作用;如果我把Title改为“325ioqukejgakshgkjashdgjka”,那这个UILabel的size会变为42,内容被截断为“32...”,因为这时frame size比IntrinsicContentSize小,Content Compression Resistancy Priority只有1,width constraint起作用,将宽度设置为42。

一个使用AutoLayout的有趣Bug

删除CenterY之前,IB知道X和Y 位置信息,因此系统给加的是ContentSizeLayoutConstraint,但删除之后,IB无法确定其Y方向的信息,生成了PrototypingLayoutConstraint。但为什么一个只有251的priority的PrototypingLayoutConstraint不会clip label呢?那我们就需要更多的了解PrototypingLayoutConstraint了。

  1. //Before
  2. 2014-07-05 23:29:40.068 AutoLayoutExample[2035:70b] current constraints (
  3. "<NSLayoutConstraint:0x8b44c20 H:|-(10)-[UILabel:0x8b450f0] (Names: '|':UITableViewCellContentView:0x8b453e0 )>",
  4. "<NSContentSizeLayoutConstraint:0x8b37520 H:[UILabel:0x8b450f0(42)] Hug:251 CompressionResistance:750>"
  5. )
  6.  
  7. //After
  8. 2014-07-05 23:32:19.251 AutoLayoutExample[2062:70b] current constraints (
  9. "<NSLayoutConstraint:0x8b66a10 H:|-(10)-[UILabel:0x8b664d0] (Names: '|':UITableViewCellContentView:0x8b66210 )>",
  10. "<NSIBPrototypingLayoutConstraint:0x8b521f0 'IB auto generated at build time for view with ambiguity' H:[UILabel:0x8b664d0(42@251)] priority:251>"
  11. )

Notes of learning AutoLayout的更多相关文章

  1. Machine Learning Algorithms Study Notes(3)--Learning Theory

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...

  2. 【转载】 DeepMind用ReinforcementLearning玩游戏

    原文地址: https://blog.csdn.net/wishchin/article/details/42425145 原文 :  http://dataunion.org/?p=639 1.引言 ...

  3. DeepMind用ReinforcementLearning玩游戏

    原文 :  http://dataunion.org/?p=639 1.引言 说到机器学习最酷的分支,非Deep learning和Reinforcement learning莫属(以下分别简称DL和 ...

  4. Machine Learning Algorithms Study Notes(2)--Supervised Learning

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...

  5. Machine Learning Algorithms Study Notes(1)--Introduction

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1    Introduction    1 1.1    ...

  6. rt-thread learning notes

    rt-thread learning notes 2018-01-15 > 001 具有相同优先级的线程,每个线程的时间片大小都可以在初始化或创建该线程时指定 rt_thread_t rt_th ...

  7. (zhuan) Notes on Representation Learning

    this blog from: https://opendatascience.com/blog/notes-on-representation-learning-1/   Notes on Repr ...

  8. Mybatis Learning Notes 1

    Mybatis Learning Notes 主要的参考是博客园竹山一叶的Blog,这里记录的是自己补充的内容 实体类属性名和数据库不一致的处理 如果是实体类的结果和真正的数据库的column的名称不 ...

  9. Rust learning notes

    Rust learning notes Rust Version 1.42.0 $ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs ...

随机推荐

  1. 引用参数,值参数,ref,out

    1,一个参数只有在引用的时候才能改变其值,这是一种情况 2,一个参数在引用后要永久的改变其值(可以用返回参数的形式) 3,多个参数在引用后要永久的改变其值或者多个参数中的部分(返回参数就适合了,因为只 ...

  2. 连续最短路算法(Successive Shortest Path)(最小费用最大流)

    #include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...

  3. [算法]检测空间三角形相交算法(Devillers & Guigue算法)

    #pragma once //GYDevillersTriangle.h /* 快速检测空间三角形相交算法的代码实现(Devillers & Guigue算法) 博客原地址:http://bl ...

  4. WordCount示例深度学习MapReduce过程(1)

    我们都安装完Hadoop之后,按照一些案例先要跑一个WourdCount程序,来测试Hadoop安装是否成功.在终端中用命令创建一个文件夹,简单的向两个文件中各写入一段话,然后运行Hadoop,Wou ...

  5. 【Office Word】论文排版有关技巧

    本文分两部分,第一部分呢是Word中标题的编号以及图表的编号:第二部分是MathType中公式编号的右对齐方法.   1. word中标题的编号以及图表的编号   本部分转载自:http://blog ...

  6. C#获取文件时间

    在NTFS下,文件的创建及修改时间可以精确到毫秒,以下是测试过程. DirectoryInfo diSource = new DirectoryInfo(@"C:\Users\不告诉你\De ...

  7. spark统计

    http://www.myexception.cn/sql/2004512.html http://blog.csdn.net/ssw_1990/article/details/52220466 ht ...

  8. Populating Tree Item With Record Group In Oracle Forms

    The below plsql program unit could be used in a WHEN-NEW-FORM-INSTANCE trigger to initially populate ...

  9. SQL Server 查询表的记录数(3种方法,推荐第一种)

    http://blog.csdn.net/smahorse/article/details/8156483 --SQL Server 查询表的记录数 --one: 使用系统表. SELECT obje ...

  10. 关于js执行顺序

    http://www.cnblogs.com/sanshi/archive/2011/02/28/1967367.html http://mtnt2008.iteye.com/blog/701981 ...