IOS AutoLayout Use

Source: Internet
Author: User


With more and more Apple models, multi-model adaptation has become a problem, some computing screen size , some with autoresize, personal Feel the best use or Apple provided AutoLayout. Of course, everyone's preferences are different, it depends on their own preferences. The AutoLayout implementation of the multi-model adaptation of the master is used to constrain the position of the control by being able to position it, provided that the size of the parent view is determined.

Now, from the simplest start, add a button to the view, and the parent view is OK for the entire screen size.

Vertical Screen Horizontal screen

Here's a look at the implementation of this code:

-(void) viewdidload {[Super viewdidload];        Additional setup after loading the view, typically from a nib.    UIButton *topleftbtn=[uibutton Buttonwithtype:uibuttontyperoundedrect];    [Topleftbtn settitle:@ "topleftbtn" forstate:uicontrolstatenormal];    [Topleftbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];   [Topleftbtn Setbackgroundcolor:[uicolor Greencolor];    Close AutoResize [topleftbtn Settranslatesautoresizingmaskintoconstraints:no];    [Self.view ADDSUBVIEW:TOPLEFTBTN]; The front constraint is also called the left constraint from the parent view Self.view the left spacing is 10 pixels nslayoutconstraint *topleftbtnleadingconstraint=[nslayoutconstraint ConstraintW ITHITEM:TOPLEFTBTN attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toItem:self.view attribute:    Nslayoutattributeleading multiplier:1 Constant:10];    [Self.view Addconstraint:topleftbtnleadingconstraint]; The top spacing on the self.view from the parent view is 10 pixels nslayoutconstraint *topleftbtntopconstraint=[nslayoutconstraint Constraintwithitem: ToPLEFTBTN attribute:nslayoutattributetop relatedby:nslayoutrelationequal toItem:self.view attribute:    Nslayoutattributetop multiplier:1 Constant:10];        [Self.view Addconstraint:topleftbtntopconstraint]; Right constrain the right margin from parent view Self.view to 10 pixels nslayoutconstraint *topleftbtnrightconstraint=[nslayoutconstraint Constraintwithitem : topleftbtn attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal toItem:self.view attribute:    Nslayoutattributetrailing multiplier:1 constant:-10];        [Self.view Addconstraint:topleftbtnrightconstraint]; The bottom spacing from the parent view Self.view is 10 pixels nslayoutconstraint *toprightbtnbottomconstraint=[nslayoutconstraint CONSTRAINTWITHITEM:TOPLEFTBTN Attribute:nslayoutattributebottom relatedby:nslayoutrelationequal ToItem:self.view    Attribute:nslayoutattributebottom multiplier:1 constant:-10]; [Self.view addconstraint:toprightbtnbottomconstraint];}
Add the above 4 constraints to determine the size of the button, OK, so simple, this is a simple, if a bit complex, then look at the following

Vertical Screen Horizontal screen

Above two buttons, below a button, the above two buttons width, all button height, all spacing is 10 pixels, the following code:

-(void) viewdidload {[Super viewdidload];        Additional setup after loading the view, typically from a nib.    UIButton *topleftbtn=[uibutton Buttonwithtype:uibuttontyperoundedrect];    [Topleftbtn settitle:@ "topleftbtn" forstate:uicontrolstatenormal];    [Topleftbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];    [Topleftbtn Setbackgroundcolor:[uicolor Greencolor];    [Topleftbtn Settranslatesautoresizingmaskintoconstraints:no];            [Self.view ADDSUBVIEW:TOPLEFTBTN];    UIButton *toprightbtn=[uibutton Buttonwithtype:uibuttontyperoundedrect];    [Toprightbtn settitle:@ "toprightbtn" forstate:uicontrolstatenormal];    [Toprightbtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];    [Toprightbtn Setbackgroundcolor:[uicolor Redcolor];    [Toprightbtn Settranslatesautoresizingmaskintoconstraints:no];        [Self.view ADDSUBVIEW:TOPRIGHTBTN];    UIButton *bottombtn=[uibutton Buttonwithtype:uibuttontyperoundedrect]; [BottoMBtn settitle:@ "bottombtn" forstate:uicontrolstatenormal];    [Bottombtn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];    [Bottombtn Setbackgroundcolor:[uicolor Bluecolor];    [Bottombtn Settranslatesautoresizingmaskintoconstraints:no];        [Self.view ADDSUBVIEW:BOTTOMBTN]; Topleftbtn the left margin of the parent view Self.view relative to the Self.view is 10 pixels nslayoutconstraint *topleftbtnleadingconstraint=[ Nslayoutconstraint constraintwithitem:topleftbtn attribute:nslayoutattributeleading relatedBy:    Nslayoutrelationequal ToItem:self.view attribute:nslayoutattributeleading multiplier:1 constant:10];        [Self.view Addconstraint:topleftbtnleadingconstraint]; TOPLEFTBTN the upper bound from the parent view Self.view relative to the Self.view is 10 pixels nslayoutconstraint *topleftbtntopconstraint=[ Nslayoutconstraint constraintwithitem:topleftbtn attribute:nslayoutattributetop relatedBy:NSLayoutRelationEqual    ToItem:self.view attribute:nslayoutattributetop multiplier:1 Constant:10]; [Self.view Addconstraint:topleftbtntopcOnstraint]; Toprightbtn with respect to topleftbtn toprightbtn and topleftbtn glazing qi Nslayoutconstraint *toprightbtntopconstraint=[ Nslayoutconstraint constraintwithitem:toprightbtn attribute:nslayoutattributetop relatedBy:NSLayoutRelationEqual    TOITEM:TOPLEFTBTN attribute:nslayoutattributetop multiplier:1 constant:0];      [Self.view Addconstraint:toprightbtntopconstraint]; Toprightbtn to the left side of the topleftbtn with respect to the TOPLEFTBTN constraint toprightbtn 10 pixels nslayoutconstraint *toprightbtnleadconstraint=[ Nslayoutconstraint constraintwithitem:toprightbtn attribute:nslayoutattributeleading relatedBy:    Nslayoutrelationequal toitem:topleftbtn attribute:nslayoutattributetrailing multiplier:1 constant:10];        [Self.view Addconstraint:toprightbtnleadconstraint]; Toprightbtn the right side of the toprightbtn to the right of the self.view with respect to the parent view is 10 pixels nslayoutconstraint *toprightbtntrailconstraint=[ Nslayoutconstraint constraintwithitem:toprightbtn attribute:nslayoutattributetrailing relatedBy: Nslayoutrelationequal ToItem:self.view AttribuTe:nslayoutattributetrailing multiplier:1 constant:-10.0];        [Self.view Addconstraint:toprightbtntrailconstraint]; TOPRIGHTBTN and topleftbtn width equal nslayoutconstraint *toprightandleftbtnwidthequalconstraint=[nslayoutconstraint CONSTRAINTWITHITEM:TOPRIGHTBTN attribute:nslayoutattributewidth relatedby:nslayoutrelationequal ToItem:TopLeftBtn    Attribute:nslayoutattributewidth multiplier:1 constant:0];        [Self.view Addconstraint:toprightandleftbtnwidthequalconstraint]; TOPRIGHTBTN and topleftbtn height equal nslayoutconstraint *toprightandleftbtnheightequalconstraint=[nslayoutconstraint CONSTRAINTWITHITEM:TOPRIGHTBTN attribute:nslayoutattributeheight relatedby:nslayoutrelationequal ToItem:    TOPLEFTBTN attribute:nslayoutattributeheight multiplier:1 constant:0];        [Self.view Addconstraint:toprightandleftbtnheightequalconstraint]; Topleftbtn and Bottombtn left align Nslayoutconstraint *bottombtnleadingconstraint=[nslayoutconstraint constraintWithItem: TOPLEFTBTN Attribute:nslayoutattributeLeading relatedby:nslayoutrelationequal toitem:bottombtn attribute:nslayoutattributeleading multiplier:1 constant:0    ];        [Self.view Addconstraint:bottombtnleadingconstraint]; Toprightbtn and Bottombtn left align Nslayoutconstraint *bottombtntrailconstraint=[nslayoutconstraint constraintWithItem: TOPRIGHTBTN attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal toItem:BottomBtn attribute:    Nslayoutattributetrailing multiplier:1 constant:0];        [Self.view Addconstraint:bottombtntrailconstraint]; The top margin of the bottombtn is constrained from the bottom of the topleftbtn to 10 pixels nslayoutconstraint *bottombtntopconstraint=[nslayoutconstraint CONSTRAINTWITHITEM:BOTTOMBTN attribute:nslayoutattributetop relatedby:nslayoutrelationequal ToItem:TopLeftBtn    Attribute:nslayoutattributebottom multiplier:1 Constant:10];        [Self.view Addconstraint:bottombtntopconstraint]; The top margin constraint of the bottombtn is 10 pixels from the bottom of the Self.view nslayoutconstraint *bottombtnbottomconstraint=[nslayoutconstraint CONSTRAINTWITHITEM:BOTTOMBTN Attribute:nslayOutattributebottom relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributebottom multiplier:1    CONSTANT:-10];        [Self.view Addconstraint:bottombtnbottomconstraint]; The height of the topleftbtn is the same as the height of bottombtn nslayoutconstraint *bottombtnheightconstraint=[nslayoutconstraint CONSTRAINTWITHITEM:TOPLEFTBTN attribute:nslayoutattributeheight relatedby:nslayoutrelationequal ToItem:BottomBtn    Attribute:nslayoutattributeheight multiplier:1 constant:0];        Multiplier Baidu is multiplier constant constant [self.view addconstraint:bottombtnheightconstraint]; Update constraint//[Self.view setneedsupdateconstraints];////[Self.view updateconstraintsifneeded];}
So much is not very convenient, then we now see how the constraint is how to achieve positioning, if the cell phone is 5s, the pixel is 640*1136 zoom out one times is 320*568

Vertical screen: Set three button height is x unknown, three button height, 10+x+10+x+10=568--->x=269, three button height are 269 pixels. The width of the BOTTOMBTN is 10+y+10=320--->y=300,bottombtn is 300 pixels wide. Now the Lord is calculating the width of the topleftbtn toprightbtn, topleftbtn and toprightbtn are the same width, then 10+z+10+z+10=320-->z=145, their width is 145 pixels, and now it's all right.

TOPLEFTBTN frame is (10,10,145,269) toprightbtn frame for (10+145+10,10,145,269)

BOTTOMBTN's frame is (10,10+269+10,145,269)

Horizontal screen, just the width of the change, the other same.

If it is 4s or 6 6plus is not the same screen size is calculated according to the above formula, the frame is not the same, so as long as you add a good constraint, you will control the position of the control, the implementation of multi-screen matching. Do not feel very studious, some people will feel that the code is written too much, indeed such a constraint is really troublesome, if you want to write less code, and want to effect good, please listen to tell


IOS AutoLayout Use

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.