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(literalsareshownincodefont;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 NSViewinthepassedviewsdictionary. |
<metricName> | ParsedasaCidentifier.ThismustbeakeymappingtoaninstanceofNSNumberinthepassedmetricsdictionary. |
<number> | Asparsedbystrtod_l,withtheClocale. |
objectOfPredicateproduction,a
viewNameis
onlyacceptableifthesubjectofthepredicateisthewidthorheightofaview.Thatis,youcanuse
[view1(==view2)]tospecifythat
view1and
view2have
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)]| |
^ |
NSDictionary*viewsDictionary=NSDictionaryOfVariableBindings(mybutton1,mybutton2);
//Getareferencetothesuperview //Createalabel UILabel*mylabel=[[UILabelalloc]init]; [mylabelsetTranslatesAutoresizingMaskIntoConstraints:NO];mylabel.text=@"MyLabel"; //Createabutton UIButton*mybutton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];[mybuttonsetTitle:@"MyButton"forState:UIControlStateNormal];[mybuttonsetTranslatesAutoresizingMaskIntoConstraints:NO]; |
//Addthebuttonandlabeltothesuperview //Gettheviewsdictionary NSDictionaryOfVariableBindings(mylabel,mybutton); |
UnderstandingtheiOS6AutoLayoutVisualFormatLanguage
//CreatetheconstraintsusingthevisuallanguageformatNSArray*constraintsArray=[NSLayoutConstraint
constraintsWithVisualFormat:@"|-[mybutton]-[mylabel(==mybutton)]-|"options:NSLayoutFormatAlignAllBaselinemetrics:nilviews:viewsDictionary];
//Workthroughthearrayofconstraints,applyingeachtothesuperviewfor(inti=0;i<constraintsArray.count;i++){
}
相关文章推荐
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- IOS Auto Layout代码实现自动布局--VFL(Visual format language)
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】
- iOS AutoLayout自动布局中级开发教程(9)-VisualFormat可视化格式字符串构成
- 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局
- AutoLayout中的Visual Format Language
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- Autolayout 中的Visual format language
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 转载自@机智的新手:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 使用Auto Layout中的VFL(Visual format language)--Swift实现