iOS visual format

来源:互联网 发布:mac app store更改用户 编辑:程序博客网 时间:2024/05/01 00:52

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 ofNSViewin the passedviews dictionary.

<metricName>

Parsed as a C identifier. This must be a key mapping to an instance ofNSNumberin the passed metrics dictionary.

<number>

As parsed bystrtod_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(100@20)]

../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


0 0
原创粉丝点击