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(100@20)]
Equal Widths
-
[button1(==button2)]
Multiple Predicates
-
[flexibleButton(>=70,<=100)]
A Complete Line of Layout
-
|-[find]-[findNext]-[findField(>=20)]-|
本人写的一个例子,地址:https://github.com/shiyingyan/autoLayout-visualFormat