Auto Layout

This article was converted by ppt shared in our team.

目标[1]

  • UILabel设置width=0为什么仍然起作用?
  • 是什么引起了布局的改变
  • three main approaches to laying out a user interface
  • The Layout Process[5]
  • 基础[7]
  • cassowary 是啥?[8]
  • UIStackView

是什么引起了布局的改变

  • External Changes(superview change)
  • The user resizes the window (OS X).
  • The device rotates (iOS).
  • The active call and audio recording bars appear or disappear (iOS).
  • You want to support different screen sizes.(非运行时)
  • ...
  • Internal Changes
  • 多语言app语言的切换(阿拉伯语更奇葩)以及日期格式的变化
  • 网络数据变化导致的改变
  • 阅读类app支持调整字体也会导致改变
  • ...

三种布局方式

  • frame布局(the most flexibility and power)
  • autoresizing masks
  • autoresizing masks adapt only to external changes.They do not support internal changes.
  • Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.

The Layout Process[5]

  • The first step – updating constraints
  • The second step – layout
  • Finally, the display pass

基础[7]

  • It’s important to note that the equations shown represent equality, not assignment.
  • There are two basic types of attributes. Size attributes (for example, Height and Width) and location attributes (for example, Leading, Left, and Top).
  • When Auto Layout solves these equations, it does not just assign the value of the right side to the left. Instead, it calculates the value for both attribute 1 and attribute 2 that makes the relationship true. This means we can often freely reorder the items in the equation.
  • Creating Nonambiguous, Satisfiable Layouts:
  • When using Auto Layout, the goal is to provide a series of equations that have one and only one possible solution.
  • In general, the constraints must define both the size and the position of each view.
  • Assuming the superview’s size is already set (for example, the root view of a scene in iOS), a nonambiguous, satisfiable layout requires two constraints per view per dimension (not counting the superview).(当然这是在使用等式前提下的大多数情况(有特殊情况),一个等式可以用两个不等式来代替,所以如果使用了不等式,这个规则可能会被打破;)
  • 约束优先级
  • 默认情况下,所有的约束都是required(优先级为1000)。Auto Layout必须计算出一个满足所有约束的解,如果不能则报错,并在控制台打印错误信息,然后AL打破一个规则重新计算解。
  • [1~1000)的优先级是可选约束,计算解时,AL按照约束优先级的顺序尝试满足所有的约束,如果不能满足某个可选约束则跳过继续计算。说的直白些就是:AL按照优先级的顺序遍历所有约束,除了required的约束必须满足外,可选约束能满足就满足,不能满足就跳过。
  • Intrinsic Content Size
  • So far, all of the examples have used constraints to define both the view’s position and its size. However, some views have a natural size given their current content. This is referred to as their intrinsic content size. For example, a button’s intrinsic content size is the size of its title plus a small margin.
  • Auto Layout represents a view’s intrinsic content size using a pair of constraints for each dimension. The content hugging pulls the view inward so that it fits snugly around the content. The compression resistance pushes the view outward so that it does not clip the content.


  • VFL[6]

With these differences in mind, the following rules apply:

  • You cannot constrain a size attribute to a location attribute.
  • 比如:在没有任何上下文的情况下,设置一个view的top等于20没有任何意义(设置了参照物);但是设置一个view的高度为20就很合理。
  • You cannot assign constant values to location attributes.
  • You cannot use a nonidentity multiplier (a value other than 1.0) with location attributes.
  • For location attributes, you cannot constrain vertical attributes to horizontal attributes.
  • For location attributes, you cannot constrain Leading or Trailing attributes to Left or Right attributes.

Cassowary

  • Auto Layout engine uses the Cassowary constraint solver.
  • Cassowary is an incremental constraint solving toolkit that efficiently solves systems of linear equalities and inequalities. Constraints may be either requirements or preferences. Client code specifies the constraints to be maintained, and the solver updates the constrained variables to have values that satisfy the constraints.

UIStackView

  • Refer to demos directly.

References

  1. https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html#//apple_ref/doc/uid/TP40010853-CH9-SW1
  2. https://blog.helftone.com/auto-layout-on-os-x/
  3. http://www.jianshu.com/p/5da3d2fe5dee
  4. http://nsomar.com/rendering-views-on-the-screen/
  5. https://www.objc.io/issues/3-views/advanced-auto-layout-toolbox/
  6. https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html#//apple_ref/doc/uid/TP40010853-CH27-SW1
  7. https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html#//apple_ref/doc/uid/TP40010853-CH9-SW1
  8. http://stacks.11craft.com/cassowary-cocoa-autolayout-and-enaml-constraints.html
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容