iOS Development AutoLayout Code implementation AutoLayout Use summary

Source: Internet
Author: User

1. Theoretical part












Demo Example 1:

UIView *blueview = [[UIView alloc] init];    Blueview.backgroundcolor = [Uicolor Bluecolor];    Do not convert Autoresizingmask to AutoLayout constraint blueview.translatesautoresizingmaskintoconstraints = NO;        [Self.view Addsubview:blueview]; Add width constraint: nslayoutconstraint *widthconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributewidth relatedby:nslayoutrelationequal Toitem:nil Attribute:nslayoutattributenotanattribute    multiplier:0.0 constant:100];        [Blueview Addconstraint:widthconstraint]; Add height constraint: nslayoutconstraint *heightconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributeheight relatedby:nslayoutrelationequal Toitem:nil Attribute:nslayoutattributenotanattribute    multiplier:0.0 constant:100];        [Blueview Addconstraint:heightconstraint]; Add right constraint: Blueview to the right of the parent control has a spacing of 10 nslayoutconstraint *rightconstraint = [Nslayoutconstraint constraintwithitem: Blueview Attribute:nslayoutattributerigHT relatedby:nslayoutrelationequal ToItem:self.view attribute:nslayoutattributeright multiplier:1.0 constant:-10];        [Self.view Addconstraint:rightconstraint]; Add top constraint: The top of Blueview is 10 of the distance from the top of the parent control nslayoutconstraint *topconstraint = [Nslayoutconstraint constraintwithitem: Blueview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toItem:self.view attribute:    Nslayoutattributetop multiplier:1.0 Constant:10]; [Self.view Addconstraint:topconstraint];

Effect:



Demo Example 2:

/** * Width height is 100, vertically horizontally centered in the parent control */-(void) test2{UIView *blueview = [[UIView alloc] init];    Blueview.backgroundcolor = [Uicolor Bluecolor];    Do not convert Autoresizingmask to AutoLayout constraint blueview.translatesautoresizingmaskintoconstraints = NO;        [Self.view Addsubview:blueview]; Add width constraint: Half of the parent control nslayoutconstraint *widthconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributewidth relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributewidth    multiplier:0.5 constant:0];        [Self.view Addconstraint:widthconstraint]; Add height constraint: Half of the parent control nslayoutconstraint *heightconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributeheight relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributeheight    multiplier:0.5 constant:0];        [Self.view Addconstraint:heightconstraint]; Horizontal center Nslayoutconstraint *centerxconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributecenterx relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecenterx    multiplier:1.0 constant:0];        [Self.view Addconstraint:centerxconstraint]; Center vertically Nslayoutconstraint *centeryconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributecentery relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecentery    multiplier:1.0 constant:0]; [Self.view addconstraint:centeryconstraint];}
Effect:

Demo Example 3:

-(void) test3{UIView *blueview = [[UIView alloc] init];    Blueview.backgroundcolor = [Uicolor Bluecolor];    Do not convert Autoresizingmask to AutoLayout constraint blueview.translatesautoresizingmaskintoconstraints = NO;        [Self.view Addsubview:blueview];    UIView *redview = [[UIView alloc] init];    Redview.backgroundcolor = [Uicolor Redcolor];    Do not convert Autoresizingmask to AutoLayout constraint redview.translatesautoresizingmaskintoconstraints = NO; [Self.view addsubview:redview];//Add width constraint: 100/************************** Blue **************************///Add height approx. Bundle: Nslayoutconstraint *heightconstraint = [Nslayoutconstraint constraintwithitem:blueview attribute: Nslayoutattributeheight relatedby:nslayoutrelationequal Toitem:nil Attribute:nslayoutattributenotanattribute    multiplier:0.0 constant:40];        [Blueview Addconstraint:heightconstraint]; Add left constraint: Blueview the left side of the parent control has a distance of 20 nslayoutconstraint *leftconstraint = [Nslayoutconstraint constraintwithitem: Blueview AttribuTe:nslayoutattributeleft relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributeleft    multiplier:1.0 CONSTANT:20];        [Self.view Addconstraint:leftconstraint]; Add right constraint: Blueview to the right of the parent control has a spacing of 20 nslayoutconstraint *rightconstraint = [Nslayoutconstraint constraintwithitem: Blueview attribute:nslayoutattributeright relatedby:nslayoutrelationequal toItem:self.view attribute:    Nslayoutattributeright multiplier:1.0 constant:-20];        [Self.view Addconstraint:rightconstraint]; Add top constraint: The top of Blueview is 20 of the distance from the top of the parent control nslayoutconstraint *topconstraint = [Nslayoutconstraint constraintwithitem: Blueview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toItem:self.view attribute:    Nslayoutattributetop multiplier:1.0 constant:20];        [Self.view Addconstraint:topconstraint]; /************************** Red **************************///Add height constraint: Blue equal height nslayoutconstraint *heightconstraint2 = [N Slayoutconstraint Constraintwithitem:redview Attribute:nSlayoutattributeheight relatedby:nslayoutrelationequal Toitem:blueview Attribute:nslayoutattributeheight    multiplier:1.0 constant:0];        [Self.view Addconstraint:heightconstraint2]; Add left constraint: redview left = = Parent control's Center x nslayoutconstraint *leftconstraint2 = [Nslayoutconstraint constraintwithitem:redview at Tribute:nslayoutattributeleft relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecenterx    multiplier:1.0 constant:0];        [Self.view Addconstraint:leftconstraint2]; Add top constraint: Redview the top of the Blueview has a distance of 20 from the bottom of the nslayoutconstraint *topconstraint2 = [Nslayoutconstraint constraintwithitem: Redview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:blueview attribute:    Nslayoutattributebottom multiplier:1.0 constant:20];        [Self.view Addconstraint:topconstraint2]; Add right constraint: redview right = = Blueview right Nslayoutconstraint *rightconstraint2 = [Nslayoutconstraint Constraintwithitem:redv Iew Attribute:nslayoutattributeright Relatedby:nslayoUtrelationequal Toitem:blueview attribute:nslayoutattributeright multiplier:1.0 constant:0]; [Self.view Addconstraint:rightconstraint2];}

Effect:




iOS Development AutoLayout Code implementation AutoLayout Use summary

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.