7. WHAT IS AUTO LAYOUT
• 一種基於約束的,描述性的佈局系統。 Auto
layout is a constraint-based, descriptive
layout system.
– 基於約束 - 以所謂相對位置的約束來定義的
– 描述性 - 使⼤ 用接近⼤ 自然語⼤ 言的⼤ 方法來進⼤
行描述
– 佈局系統 - 設計界⼤ 面的各個元素的位置
• 使用約束條件來描述佈局,view的frame會依
據這些約束來進行計算。
• Describe the layout with constraints, and
frames are calculated automatically.
8. THINKING OF AUTO LAYOUT
• 使用Auto Layout,我們關心的是每個view
之間的關聯。這種設計方式我們叫做「通
過目的來設計」,要表達的是要達到的目
標是什麼,而不是如何來完成目標。
• 「這個按鈕的左上角的坐標是(20, 20)」,
現在你可以這麼表達了「這個按鈕在它的
super view中垂直居中,把它放置在離它
的super view左邊緣的一個固定位置。」
15. NSLayoutConstraint 動態產生 Auto
Layout 的效果
/* Create an array of constraints using an ASCII art-like visual format string. */
• + (NSArray *)constraintsWithVisualFormat:(NSString
*)format options:(NSLayoutFormatOptions)opts
metrics:(NSDictionary *)metrics views:(NSDictionary
*)views;
/* Create constraints explicitly. Constraints are of the form "view1.attr1 = view2.attr2 * multiplier
+ constant” If your equation does not have a second view and attribute, use nil and
NSLayoutAttributeNotAnAttribute. */
• +(id)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation toItem:(id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier constant:(CGFloat)c;
22. 參考
• Furnace iOS 程式設計中文學習網站: Auto Layout
• Auto Layout介紹
• Auto Layout Keynote
• 10 Things You Need To Know About Cocoa Auto
Layout
• Video Tutorial: Beginning Auto Layout
• Beginning Auto Layout Tutorial in iOS 7: Part 1
• iOS/Mac Autolayout Constraints