Fit----AutoLayout

Source: Internet
Author: User

Autlayout relative layout, define your location based on the location of the referenced view. Assign a position on the screen by constraining the relationship between the view and the view, usually with the VFL language

The VFL (Visual Format Language) Visual format language, which constrains the relationship between characters and characters through strings

Use autlayout must disable translatesautoresizingmaskintoconstraints to determine its position using the size of the frame origin itself. and AutoLayout define your location based on the location of the reference view.

AutoLayout is the relative layout constraint view and the relationship between views to assign a position on the screen

to constrain the relationship between views and views by adding strings using the VFL (visual format Language Visual formatting language)

Use AutoLayout You must disable translatesautoresizingmaskintoconstraints before you can use the

Relative layout is to find a reference to use reference as the basis, set his relative distance with reference to set their own position

The VFL must have a two-way constraint.

H: mean: Landscape

V: Indicates: Vertical

| 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)

[] Represents a 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

Minimum size equipment as a benchmark when using AutoLayout adaptation

/*//VFL Horizontal Vertical layout

@ "H:" To set landscape layout

@ "V:" To set the vertical layout

Set landscape layout distance to the left margin of the reference view

@ "h:|-20-"

@ "h:[view]-20-"

@ "H:|-20-[view (+)]" view width is always 200

@ "H:|-20-[view (otherview)]" The width of the view is the same as the width of Otherview

@ "H:|-20-[view (>=200)]" Set landscape layout distance to the left margin of the reference view Set view landscape dimensions cannot be less than 200

@ "H:|-20-[view (>=200)]-20-|" Set landscape layout distance to the left margin of the reference view Set view landscape dimensions cannot be less than 200 set the spacing between the right and the reference view

*/

When a view uses a property, the binding key needs to bind its real name _titlelable

Self.titlelable = _titlelable

Here's the code:

1. Use AutoLayout to layout a view

Write the following code in Viewdidload:

UIView *view = [[UIView alloc]init];

Use AutoLayout must disable Translatesautoresizingmaskintoconstraints

View.translatesautoresizingmaskintoconstraints =no;

View.backgroundcolor =[uicolor Redcolor];

[Self.view Addsubview:view];

Nsdictionary *views =nsdictionaryofvariablebindings (view);

[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat: @ "H:|-20-[view (>=200)]-20-|" options : 0 Metrics:nil Views:views];

[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@ "V:|-40-[view (>=200)]-20-|" options:0 Metrics:nil Views:views]];

2. Use AutoLayout layout two views:

UIView *view = [[UIView alloc]init];

View.translatesautoresizingmaskintoconstraints =no;

View.backgroundcolor =[uicolor Redcolor];

[Self.view Addsubview:view];

UIView *view1 = [[UIView alloc]init];

View1.translatesautoresizingmaskintoconstraints =no;

View1.backgroundcolor =[uicolor Yellowcolor];

[Self.view Addsubview:view1];

Nsdictionary *views = nsdictionaryofvariablebindings (view,view1);

[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@ "H:|-20-[view (>=200)]-20-|" options : 0 Metrics:nil Views:views];

[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat: @ "V:|-40-[view (>=50)]" options:0 Metrics:nil Views:views]];

[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat: @ "H:|-20-[view1 (>=200)]-20-|" options:0 Metrics:nil Views:views]];

[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat: @ "V:[view]-10-[view1 ()]" options:0 Metrics:nil Views:views]];

3. Use AutoLayout to layout multiple views

Nsarray *colorlist = @[[uicolor redcolor],[uicolor yellowcolor],[uicolor Greencolor]];

for (int i=0; i<3; i++) {

UIView *view =[[uiview Alloc]init];

View.backgroundcolor = Colorlist[i];

View.tag = 30+i;

View.translatesautoresizingmaskintoconstraints = NO;

[Self.view Addsubview:view];

}

UIView *redcolorview = [Self.view viewwithtag:30];

UIView *yellowcolorview = [Self.view viewwithtag:31];

UIView *greencolorview = [Self.view viewwithtag:32];

Nsdictionary * views = nsdictionaryofvariablebindings (Yellowcolorview,redcolorview,greencolorview);

Nsarray * hlist = @[@ "H:|-20-[redcolorview (>=200)]-20-|", @ "H:|-20-[yellowcolorview (>=100)]-10-[ Greencolorview (Yellowcolorview)]-20-| "];

Vertical relationship between red view and yellow view and red view and green view

Nsarray *vlist = @[@ "V:|-40-[redcolorview (]-10-[yellowcolorview) Redcolorview", @ "V:|-40-[redcolorview (50)]-10 -[greencolorview (Redcolorview)] ";

for (int i =0; i<vlist.count; i++) {

[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:hlist[i] options:0 metrics:nil Views: views]];

[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:vlist[i] options:0 metrics:nil Views: views]];

}

Fit----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.