iOS little thing about the VFL

Source: Internet
Author: User

VFL:Visual Format language

Writing:

Functional expressions

Horizontal direction H:

Vertical direction V:

views [view]

Superview |

Relationship >=,==,<=

Space, Gap-

Priority @value

It is important to note that in the code created by UIView, be sure to add the following code

Xxx.translatesautoresizingmaskintoconstraints=no;

If you do not have the above line, your constraint will not take effect, and the console will output a series of errors.


The specific code:

//1. Adding 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.VFL build constraint

nsdictionary *mertrics = @{@ "margin" : @20}; // parameter \ value

nsdictionary *views = nsdictionaryofvariablebindings(Blueview, redview);

nsarray *conts = [nslayoutconstraint constraintswithvisualformat:@ "h:|-margin-[ Blueview]-margin-[redview (==blueview)]-margin-| " options:nslayoutformatalignalltop | nslayoutformatalignallbottom metrics: mertrics views: views];

nsarray *conts2 = [nslayoutconstraint constraintswithvisualformat:@ "v:[blueview (= = Blueheight)]-margin-| " options:0 metrics:@{@ "Blueheight" : @40, @ "margin" : @20} views : views];

[self. View addconstraints: conts];

[self. View addconstraints: conts2];

UIButton *button=[[UIButton alloc]init];

button=[UIButton buttonwithtype:uibuttontypesystem];

[Button settitle:@ "VFL" forstate:uicontrolstatenormal];

button. translatesautoresizingmaskintoconstraints=NO;

[Button setbackgroundcolor: [uicolor blackcolor];

[self. View addsubview: button];

    nsarray *hconstraint =[ nslayoutconstraint constraintswithvisualformat :@ "h:|-20-[button]-200-|" options:0 metrics:nil views :@{@ "button" :button}

    nsarray *vconstraint = [ nslayoutconstraint constraintswithvisualformat :@ "v:|-300-[button]-200-|" options:0 metrics:nil views :@{@ "button" :button}

[self. View addconstraints: hconstraint];

[self. View addconstraints: vconstraint];

iOS little thing about the VFL

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.