1. 程式人生 > >iOS visual format

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


上面的表格 你也許看的不太懂,要多讀多寫幾個例子,自然而然就明白了。

下面是官方使用visual format的幾個簡單例子

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]

../Art/standardSpace.png
Width Constraint

[button(>=50)]

../Art/widthConstraint.png
Connection to Superview

|-50-[purpleBox]-50-|

../Art/connectionToSuperview.png
Vertical Layout

V:[topField]-10-[bottomField]

../Art/verticalLayout.png
Flush Views

[maroonView][blueView]

../Art/flushViews.png
Priority

[button([email protected])]

../Art/priority.png
Equal Widths

[button1(==button2)]

../Art/equalWidths.png
Multiple Predicates

[flexibleButton(>=70,<=100)]

../Art/multiplePredicates.png
A Complete Line of Layout

|-[find]-[findNext]-[findField(>=20)]-|

../Art/completeLayout.png

本人寫的一個例子,地址:https://github.com/shiyingyan/autoLayout-visualFormat