Auto Layout

This article was converted by ppt shared in our team.

目標[1]

  • UILabel設(shè)置width=0為什么仍然起作用源譬?
  • 是什么引起了布局的改變
  • three main approaches to laying out a user interface
  • The Layout Process[5]
  • 基礎(chǔ)[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語言的切換(阿拉伯語更奇葩)以及日期格式的變化
  • 網(wǎng)絡(luò)數(shù)據(jù)變化導致的改變
  • 閱讀類app支持調(diào)整字體也會導致改變
  • ...

三種布局方式

  • 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

基礎(chǔ)[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).(當然這是在使用等式前提下的大多數(shù)情況(有特殊情況)累澡,一個等式可以用兩個不等式來代替苛败,所以如果使用了不等式浙踢,這個規(guī)則可能會被打破勋眯;)
  • 約束優(yōu)先級
  • 默認情況下唬复,所有的約束都是required(優(yōu)先級為1000)。Auto Layout必須計算出一個滿足所有約束的解蔓倍,如果不能則報錯悬钳,并在控制臺打印錯誤信息,然后AL打破一個規(guī)則重新計算解偶翅。
  • [1~1000)的優(yōu)先級是可選約束默勾,計算解時,AL按照約束優(yōu)先級的順序嘗試滿足所有的約束聚谁,如果不能滿足某個可選約束則跳過繼續(xù)計算母剥。說的直白些就是:AL按照優(yōu)先級的順序遍歷所有約束,除了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.
  • 比如:在沒有任何上下文的情況下朵耕,設(shè)置一個view的top等于20沒有任何意義(設(shè)置了參照物)炫隶;但是設(shè)置一個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.reibang.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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末阎曹,一起剝皮案震驚了整個濱河市伪阶,隨后出現(xiàn)的幾起案子煞檩,更是在濱河造成了極大的恐慌,老刑警劉巖望门,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件形娇,死亡現(xiàn)場離奇詭異锰霜,居然都是意外死亡筹误,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門癣缅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來厨剪,“玉大人,你說我怎么就攤上這事友存〉簧牛” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵屡立,是天一觀的道長直晨。 經(jīng)常有香客問我,道長膨俐,這世上最難降的妖魔是什么勇皇? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮焚刺,結(jié)果婚禮上敛摘,老公的妹妹穿的比我還像新娘。我一直安慰自己乳愉,他們只是感情好兄淫,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蔓姚,像睡著了一般捕虽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上坡脐,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天薯鳍,我揣著相機與錄音,去河邊找鬼挨措。 笑死挖滤,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的浅役。 我是一名探鬼主播斩松,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼觉既!你這毒婦竟也來了惧盹?” 一聲冷哼從身側(cè)響起乳幸,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎钧椰,沒想到半個月后粹断,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡嫡霞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年瓶埋,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片诊沪。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡养筒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出端姚,到底是詐尸還是另有隱情晕粪,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布渐裸,位于F島的核電站巫湘,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏昏鹃。R本人自食惡果不足惜尚氛,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盆顾。 院中可真熱鬧怠褐,春花似錦、人聲如沸您宪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宪巨。三九已至磷杏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間捏卓,已是汗流浹背极祸。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留怠晴,地道東北人遥金。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像蒜田,于是被迫代替她去往敵國和親稿械。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容