Http://www.cnblogs.com/chars/p/5146607.html
What is the VFL language
The VFL (visual format Language), "Visual formatting language".
The VFL is an abstract language that Apple has introduced to simplify AutoLayout coding.
Syntax description
H:[cancelbutton (]-12-[acceptbutton)]cancelbutton width 72,acceptbutton width 50, between them a distance of H:[wideview (>[email Protected])]wideview width greater than or equal to 60point, the constraint priority is 700 (the priority is the maximum value of 1000, the higher the precedence of the constraint is first satisfied) V:[redbox][yellowbox (==redbox) ] in the vertical direction, there is a redbox, immediately below a height equal to Redbox height of yellowbox h:|-10-[find]-[findnext]-[findfield (>=20)]-| horizontal direction, The find distance from the left edge of the parent view is 10, followed by the FindNext distance from the Find interval default width, and then findfield with a width of not less than 20, which is the default width of FindNext and the right edge of the parent view. (Vertical bar "|" Represents the edge of the Superview).
How to use
Use the VFL to create an array of Constraints + (Nsarray *) Constraintswithvisualformat: (NSString *) format options: (nslayoutformatoptions) OPTs metrics :(nsdictionary *) metrics Views: (Nsdictionary *) views; FORMAT:VFL Statement opts: The control used in the VIEWS:VFL statement of the specific value used in the constraint type METRICS:VFL statement Create a dictionary (internally containing the controls used in the VFL statement) to define the Nsdictionaryofvariablebindings (...) shortcut macro.
Example Show
As follows:
Implementation code:
-(void) horizontallayout{//1. Add two controls UIView *blueview = [[UIView alloc] init]; Blueview.backgroundcolor = [Uicolor Bluecolor]; Blueview.translatesautoresizingmaskintoconstraints = NO; [Self.view Addsubview:blueview]; UIView *redview = [[UIView alloc] init]; Redview.backgroundcolor = [Uicolor Redcolor]; Redview.translatesautoresizingmaskintoconstraints = NO; [Self.view Addsubview:redview]; 2. Add constraint//2.1 horizontal direction constraint nsstring *HVFL = @ "H:|-30-[blueview]-30-[redview (==blueview)]-30-|"; Nsarray *hcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:HVFL options:nslayoutformatalignallbottom | Nslayoutformatalignalltop metrics:nil views:@{@ "Blueview": Blueview, @ "Redview": Redview}]; [Self.view addconstraints:hcons]; 2.2 Constraints of the vertical direction nsstring *VVFL = @ "V:[blueview (50)]-30-|"; Nsarray *vcons = [nslayoutconstraint constraintswithvisualformat:vvfl options:0 metrics:nil views:@{@ "BlueView": Blueview}]; [Self.view addconstraints:vcons];} -(void) verticallayout{//1. Add two controls UIView *blueview = [[UIView alloc] init]; Blueview.backgroundcolor = [Uicolor Bluecolor]; Blueview.translatesautoresizingmaskintoconstraints = NO; [Self.view Addsubview:blueview]; UIView *redview = [[UIView alloc] init]; Redview.backgroundcolor = [Uicolor Redcolor]; Redview.translatesautoresizingmaskintoconstraints = NO; [Self.view Addsubview:redview]; 2. Add constraint//2.1 horizontal direction constraint nsstring *HVFL = @ "h:|-30-[blueview]-30-|"; Nsarray *hcons = [nslayoutconstraint constraintswithvisualformat:hvfl options:0 metrics:nil views:@{@ "BlueView": Blueview}]; [Self.view addconstraints:hcons]; 2.2 Vertical constraint NSString *VVFL = @ "V:|-30-[blueview (]-30-[redview (==blueview)]"; Nsarray *vcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:VVFL options:nslayoutformatalignallright metrics: Nil views:@{@ "Blueview": Blueview, @ "Redview": Redview}]; [Self.view addconstraints:vcons]; Nslayoutconstraint *redleftcon = [nslayoutconsTraint Constraintwithitem:redview attribute:nslayoutattributeleft relatedby:nslayoutrelationequal ToItem:blueView Attribute:nslayoutattributecenterx multiplier:1.0 constant:0]; [Self.view Addconstraint:redleftcon];}
Summary
Finally, a summary of the format string is presented:
Function |
An expression |
Horizontal direction |
H: |
Vertical direction |
V: |
Views |
[View] |
Superview |
| |
Relationship |
>=,==,<= |
Space, Gap |
- |
Priority level |
@value |
Introduction to the VFL language for iOS learning