First knowledge of AutoLayout

Source: Internet
Author: User

First, use "formula":

1, Frame: The origin and its position to determine its position

2. AutoLayout: Define your location according to the location of the reference view

3, AutoLayout: Relative layout constrains the relationship between views and views to assign a position on the screen

4. Use the VFL (visual format Language Visual formatting language) to constrain the relationship between views and views by adding characters

5, the use of AutoLayout must be translatesautoresizingmaskintoconstraints disabled to use the relative layout is to find a reference to take reference as the basis, set his relative distance with reference to set their own position

6, the FVL need to have a two-direction constraint (1, "H:" (horizontal),

2, "V:" (vertical),

3, "|" (represents its parent view),

4, " -50-" (represents the spacing between two views),

5, "[TextField]")

7, H: Transverse

| Represents his parent view

-50-represents the distance from the previous view (the rear view is TextField, and the front view is his parent view)

[TextField (>=200)] to constrain the width of the view (>=200) allows the minimum width is 200 if the vertical is to allow the smallest height

@ "H:|-50-[textfield (>=200)]-50-|"

Distance from sitting Edge 50 right border distance 50 allows the minimum width of the view to be 200

8, the use of AutoLayout adaptation to the minimum size equipment as the benchmark

Second, use example:

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One [self DEMO3]; A      - } -  the //a View -- (void) Demo1 { -UIView *view =[[UIView alloc] init]; - //Disable the use of the Translatesautoresizingmaskintoconstraints property if you use AutoLayout +      -View.translatesautoresizingmaskintoconstraints =NO; +View.backgroundcolor =[Uicolor Redcolor]; A [Self.view Addsubview:view]; at      -      - //the VFL horizontal vertical layout - //@ "H:" To set landscape layout - //@ "h:|-20-" sets the horizontal layout distance to the left margin of the parent view - //@ "H:|-20-[view (>=200)]" Sets the horizontal layout distance to the left margin of the parent view, setting the view landscape dimensions cannot be less than in //@ "H:|-20-[view (>=200)]-20-|" Sets the horizontal layout distance to the left margin of the parent view, setting the view landscape size cannot be less than 200 set the spacing between the right and the parent view -      to      + //@ "V:|-40-[view (>=200)]-20-|" Set vertical layout distance from top margin 40, set view size to no less than 400, set margin between bottom and parent view - //The use of the VFL requires that the object (view) of the view be bound to his name (the string) . theNsdictionary *views =nsdictionaryofvariablebindings (view); * //adding constraints to Self.view and view $ //addconstraints ways to add constraintsPanax Notoginseng //nslayoutconstraint A class that adds a specific constraint -      the //+ (Nsarray *) Constraintswithvisualformat: (NSString *) format options: (nslayoutformatoptions) OPTs metrics: ( Nsdictionary *) Metrics Views: (Nsdictionary *) views; + //format VFL; A //opts agreed to layout in a certain direction; the //metrics The parameters of the binding; + //views-bound View parameters -[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"H:|-20-[view (>=200)]-20-|"Options0Metrics:nil Views:views]]; $[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:|-40-[view (>=200)]-20-|"Options0Metrics:nil Views:views]]; $      - } -  the  -- (void) Demo2 {WuyiUIView *view =[[UIView alloc] init]; the     //Disable the use of the Translatesautoresizingmaskintoconstraints property if you use AutoLayout -      WuView.translatesautoresizingmaskintoconstraints =NO; -View.backgroundcolor =[Uicolor Redcolor]; About [Self.view Addsubview:view]; $      -      -UIView *view1 =[[UIView alloc] init]; -     //Disable the use of the Translatesautoresizingmaskintoconstraints property if you use AutoLayout A      +View1.translatesautoresizingmaskintoconstraints =NO; theView1.backgroundcolor =[Uicolor Browncolor]; - [Self.view Addsubview:view1]; $      the  theNsdictionary *views =nsdictionaryofvariablebindings (view,view1); the //horizontal constraints on Red view the[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"H:|-20-[view (>=200)]-20-|"Options0Metrics:nil Views:views]]; - //vertical constraint for red view in[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:|-40-[view ()]-10-[view1]"Options0Metrics:nil Views:views]]; the      the //horizontal restraint of brown view1 About[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"h:|-20-[view1 (>=200)]-20-|"Options0Metrics:nil Views:views]]; the //vertical restraint of brown view1 the[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:[view]-10-[view1 ()"Options0Metrics:nil Views:views]]; the      +      -UIView *view2 =[[UIView alloc] init]; theView2.translatesautoresizingmaskintoconstraints =NO;BayiView2.backgroundcolor =[Uicolor Greencolor]; the [Self.view addsubview:view2]; the //View2 Landscape - //View2 Vertical - } the  the //optimize Demo2 the- (void) Demo3 { theUIView *view =[[UIView alloc] init]; -     //Disable the use of the Translatesautoresizingmaskintoconstraints property if you use AutoLayout the      theView.translatesautoresizingmaskintoconstraints =NO; theView.backgroundcolor =[Uicolor Redcolor];94 [Self.view Addsubview:view]; the      the      theUIView *view1 =[[UIView alloc] init];98     //Disable the use of the Translatesautoresizingmaskintoconstraints property if you use AutoLayout About      -View1.translatesautoresizingmaskintoconstraints =NO;101View1.backgroundcolor =[Uicolor Browncolor];102 [Self.view Addsubview:view1];103     104      theNsdictionary *views =nsdictionaryofvariablebindings (view,view1);106     //horizontal constraints on Red view107[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"H:|-20-[view (>=200)]-20-|"Options0Metrics:nil Views:views]];108     //vertical constraint of red view Brown View1109 //[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@ "V:|-40-[view ()]-10-[view1 (50) ] "options:0 Metrics:nil views:views]; the     111 //Red View Brown view1 the height of two views is the //[View1 (view)];113[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:|-40-[view ()]-10-[view1 (view)]"Options0Metrics:nil Views:views]]; the      the     //horizontal restraint of brown view1 the[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"h:|-20-[view1 (>=200)]-20-|"Options0Metrics:nil Views:views]];117     118 //    //vertical restraint of brown view1119 //[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@ "V:[view]-10-[view1 ()]" options : 0 metrics:nil views:views]; -     121 122}

Third, simulator operation

Demo1 Operation Effect:

Demo2 Operation Effect:

Demo3 Operation Effect:

First knowledge of AutoLayout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.