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 (-[acceptbutton)-]cancelbutton width 72,acceptbutton width 50, The spacing between them is H:[wideview (>=)]wideview width is greater than or equal to 60point, the constraint priority is 700 (the priority is the maximum value is 1000, The higher the precedence of the constraint is satisfied first v:[redbox][yellowbox (= =RedBox)] vertically, there is a redBox, immediately below a height equal to the redBox height of Yellowbox H:|--[find]-[findnext]-[findfield (>=)]-| horizontally, 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
:
Implementation code:
-(void) horizontallayout{//1. Add two controlsUIView *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. Adding Constraints//2.1 Constraints in the horizontal directionNSString *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 in the vertical directionNSString *VVFL =@"V:[blueview (]-30-|)"; Nsarray*vcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:VVFL options:0Metrics:nil views:@{@"Blueview": Blueview}]; [Self.view addconstraints:vcons];}-(void) verticallayout{//1. Add two controlsUIView *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. Adding Constraints//2.1 Constraints in the horizontal directionNSString *HVFL =@"h:|-30-[blueview]-30-|"; Nsarray*hcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:HVFL options:0Metrics:nil views:@{@"Blueview": Blueview}]; [Self.view addconstraints:hcons]; //2.2 Constraints in the vertical directionNSString *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.0constant0]; [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 |
Reference: http://www.cnblogs.com/chars/p/5146607.html
IOS UI Layout-vfl language