iOS visual format
autoLayout的學習資源實在太少,下面是我個人這一段時間對autoLayout學習的總結。
cocoa & cocoa touch 有三種方式建立自動佈局:xib || storyBoard、使用程式碼建立單個約束、使用visual format建立約束.
關於三種方式的比較,可以在internet找到答案。官方指出的visual format 語法如下表格:
Symbol |
Replacement rule |
<visualFormatString> |
(<orientation>:)?(<superview><connection>)?<view>(<connection><view>)*(<connection><superview>)? |
<orientation> |
H|V (H->表示水平方向,V->表示垂直方向) |
<superview> |
| (這條豎線 代表 當前檢視的父檢視) |
<view> |
[<viewName>(<predicateListWithParens>)?] 例:[someView(someViewWidth)] |
<connection> |
e|-<predicateList>-|- |
<predicateList> |
<simplePredicate>|<predicateListWithParens> |
<simplePredicate> |
<metricName>|<positiveNumber> |
<predicateListWithParens> |
(<predicate>(,<predicate>)*) |
<predicate> |
(<relation>)?(<objectOfPredicate>)(@<priority>)? |
<relation> |
==|<=|>= |
<objectOfPredicate> |
<constant>|<viewName> (see note) |
<priority> |
<metricName>|<number> |
<constant> |
<metricName>|<number> |
<viewName> |
Parsed as a C identifier. This must be a key mapping to an instance of NSView in the passedviews dictionary. |
<metricName> |
Parsed as a C identifier. This must be a key mapping to an instance ofNSNumber in the passed metrics dictionary. |
<number> |
As parsed by strtod_l, with the C locale |
上面的表格 你也許看的不太懂,要多讀多寫幾個例子,自然而然就明白了。
The following are examples of constraints you can specify using the visual format. Note how the text visually matches the image.
- Standard Space
-
[button]-[textField]
- Width Constraint
-
[button(>=50)]
- Connection to Superview
-
|-50-[purpleBox]-50-|
- Vertical Layout
-
V:[topField]-10-[bottomField]
- Flush Views
-
[maroonView][blueView]
- Priority
-
[button([email protected])]
- Equal Widths
-
[button1(==button2)]
- Multiple Predicates
-
[flexibleButton(>=70,<=100)]
- A Complete Line of Layout
-
|-[find]-[findNext]-[findField(>=20)]-|
本人寫的一個例子,地址:https://github.com/shiyingyan/autoLayout-visualFormat