iOS 6 Auto Layout Visual Format Language

Visual Format Language

Visual Format Syntax

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]

image: ../Art/standardSpace.png
Width Constraint

[button(>=50)]

image: ../Art/widthConstraint.png
Connection to Superview

|-50-[orchidBox]-50-|

image: ../Art/connectionToSuperview.png
Vertical Layout

V:[topField]-10-[bottomField]

image: ../Art/verticalLayout.png
Flush Views

[maroonView][oceanView]

image: ../Art/flushViews.png
Priority

[button(100@20)]

image: ../Art/priority.png
Equal Widths

[button1(==button2)]

image: ../Art/equalWidths.png
Multiple Predicates

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

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

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

image: ../Art/completeLayout.png

The notation prefers good visualization over completeness of expressibility. There are constraints that cannot be expressed in visual format syntax, although most of the constraints that are useful in real user interfaces can be. One useful constraint that cannot be expressed is a fixed aspect ratio (for example, imageView.width = 2 * imageView.height). To create such a constraint, you must useconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.

Visual Format String Grammar

The visual format string grammar is defined as follows (literals are shown in code fonte denotes the empty string).

Symbol

Replacement rule

<visualFormatString>

(<orientation>:)?

(<superview><connection>)?

<view>(<connection><view>)*

(<connection><superview>)?

<orientation>

H|V

<superview>

|

<view>

[<viewName>(<predicateListWithParens>)?]

<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 passed views dictionary.

<metricName>

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

<number>

As parsed by strtod_l, with the C locale.

Note: For the objectOfPredicate production, a viewName is only acceptable if the subject of the predicate is the width or height of a view. That is, you can use [view1(==view2)] to specify that view1 and view2 have the same width.

If you make a syntactic mistake, an exception is thrown with a diagnostic message. For example:

Expected ':' after 'V' to specify vertical arrangement
V|[backgroundBox]|
 ^
 
A predicate on a view's thickness must end with ')' and the view must end with ']'
|[whiteBox1][blackBox4(blackWidth][redBox]|
                                 ^
 
Unable to find view with name blackBox
|[whiteBox2][blackBox]
                     ^
 
Unknown relation. Must be ==, >=, or <=
V:|[blackBox4(>30)]|
               ^


Some sample code to create views and then specify multiple constraints using a visual format languagestring, would, therefore, read as follows:

page159image12760

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (mybutton1,mybutton2);

// Get a reference to the superview
UIView *superview = self.view;

//Create a label
UILabel *mylabel = [[UILabel alloc]init];

[mylabel setTranslatesAutoresizingMaskIntoConstraints:NO];mylabel.text = @"My Label";

//Create a button
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];[mybutton setTitle:@"My Button" forState:UIControlStateNormal];[mybutton setTranslatesAutoresizingMaskIntoConstraints:NO];

//Add the button and label to the superview
[superview addSubview:mylabel];
[superview addSubview:mybutton];
// Get the views dictionary
NSDictionary *viewsDictionary =

NSDictionaryOfVariableBindings(mylabel, mybutton);

page159image20296


Understanding the iOS 6 Auto Layout Visual Format Language

//Create the constraints using the visual language formatNSArray *constraintsArray = [NSLayoutConstraint

constraintsWithVisualFormat:@"|-[mybutton]-[mylabel(==mybutton)]-|"options:NSLayoutFormatAlignAllBaseline metrics:nilviews:viewsDictionary];

//Work through the array of constraints, applying each to the superviewfor (int i = 0; i<constraintsArray.count; i++) {


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值