您的位置:首页 > 移动开发 > IOS开发

iOS 6 Auto Layout Visual Format Language

2013-02-28 11:56 429 查看


VisualFormatLanguage


VisualFormatSyntax

Thefollowingareexamplesofconstraintsyoucanspecifyusingthevisualformat.Notehowthetextvisuallymatchestheimage.

StandardSpace
[button]-[textField]



WidthConstraint
[button(>=50)]



ConnectiontoSuperview
|-50-[orchidBox]-50-|



VerticalLayout
V:[topField]-10-[bottomField]



FlushViews
[maroonView][oceanView]



Priority
[button(100@20)]



EqualWidths
[button1(==button2)]



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



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




Thenotationprefersgoodvisualizationovercompletenessofexpressibility.Thereareconstraintsthatcannotbeexpressedinvisualformatsyntax,althoughmostoftheconstraintsthatareusefulinrealuser
interfacescanbe.Oneusefulconstraintthatcannotbeexpressedisafixedaspectratio(forexample,
imageView.width=2*imageView.height
).Tocreatesuchaconstraint,
youmustuse
constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
.


VisualFormatStringGrammar

Thevisualformatstringgrammarisdefinedasfollows(literalsareshownin
codefont
;edenotestheempty
string).

Symbol
Replacementrule
<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>(seenote)
<priority>
<metricName>|<number>
<constant>
<metricName>|<number>
<viewName>
ParsedasaCidentifier.

Thismustbeakeymappingtoaninstanceof
NSView
inthepassedviewsdictionary.
<metricName>
ParsedasaCidentifier.Thismustbeakeymappingtoaninstanceof
NSNumber
inthepassedmetricsdictionary.
<number>
Asparsedby
strtod_l
,withtheClocale.
Note:Forthe
objectOfPredicate
production,a
viewName
is
onlyacceptableifthesubjectofthepredicateisthewidthorheightofaview.Thatis,youcanuse
[view1(==view2)]
tospecifythat
view1
and
view2
have
thesamewidth.

Ifyoumakeasyntacticmistake,anexceptionisthrownwithadiagnosticmessage.Forexample:

Expected':'after'V'tospecifyverticalarrangement

V|[backgroundBox]|

^


Apredicateonaview'sthicknessmustendwith')'andtheviewmustendwith']'

|[whiteBox1][blackBox4(blackWidth][redBox]|

^


UnabletofindviewwithnameblackBox

|[whiteBox2][blackBox]

^


Unknownrelation.Mustbe==,>=,or<=

V:|[blackBox4(>30)]|

^

Somesamplecodetocreateviewsandthenspecifymultipleconstraintsusingavisualformatlanguagestring,would,therefore,readasfollows:



NSDictionary*viewsDictionary=NSDictionaryOfVariableBindings(mybutton1,mybutton2);

//Getareferencetothesuperview
UIView*superview=self.view;

//Createalabel

UILabel*mylabel=[[UILabelalloc]init];

[mylabelsetTranslatesAutoresizingMaskIntoConstraints:NO];mylabel.text=@"MyLabel";

//Createabutton

UIButton*mybutton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];[mybuttonsetTitle:@"MyButton"forState:UIControlStateNormal];[mybuttonsetTranslatesAutoresizingMaskIntoConstraints:NO];

//Addthebuttonandlabeltothesuperview
[superviewaddSubview:mylabel];
[superviewaddSubview:mybutton];

//Gettheviewsdictionary
NSDictionary*viewsDictionary=

NSDictionaryOfVariableBindings(mylabel,mybutton);



UnderstandingtheiOS6AutoLayoutVisualFormatLanguage

//CreatetheconstraintsusingthevisuallanguageformatNSArray*constraintsArray=[NSLayoutConstraint

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

//Workthroughthearrayofconstraints,applyingeachtothesuperviewfor(inti=0;i<constraintsArray.count;i++){

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐