iOS Auto layout

Source: Internet
Author: User

Introduction to iOS Auto-layout

Automatic layouts are introduced in iOS 6.0 and can only support IOS6.0 and later versions. It can help us create an interface for multiple kinds of devices.

Instance steps

1. Create a simple View based application

2. Modify the contents of the VIEWCONTROLLER.M file as shown below

#import "ViewController.h" @interface Viewcontroller () @property (nonatomic, strong) UIButton *leftbutton; @property ( Nonatomic, Strong) UIButton *rightbutton, @property (nonatomic, strong) Uitextfield *textfield; @end @implementation    viewcontroller-(void) viewdidload{[Super Viewdidload];    UIView *superview = Self.view; /*1.    Create LeftButton and add to our view*/Self.leftbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];    Self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;    [Self.leftbutton settitle:@ "LeftButton" forstate:uicontrolstatenormal];        [Self.view AddSubview:self.leftButton]; /* 2. Constraint to position LeftButton ' s x*/nslayoutconstraint *leftbuttonxconstraint = [Nslayoutconstraint constraintw IthItem:self.leftButton Attribute:nslayoutattributecenterx relatedby:nslayoutrelationgreaterthanorequal ToItem:    Superview Attribute:nslayoutattributecenterx multiplier:1.0 constant:-60.0f]; /* 3. Constraint to position LeftbuttOn ' s y*/nslayoutconstraint *leftbuttonyconstraint = [Nslayoutconstraint ConstraintWithItem:self.leftButton Attribu Te:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:superview attribute:nslayoutattributecentery mu       ltiplier:1.0f constant:0.0f]; /* 4. ADD the Constraints to button ' s superview*/[Superview addconstraints:@[Leftbuttonxconstraint, Leftbuttonyconstrain        T]]; /*5.    Create Rightbutton and add to our view*/Self.rightbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];    Self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;    [Self.rightbutton settitle:@ "Rightbutton" forstate:uicontrolstatenormal];        [Self.view AddSubview:self.rightButton]; /*6. Constraint to position Rightbutton ' s x*/nslayoutconstraint *rightbuttonxconstraint = [Nslayoutconstraint constrain TWithItem:self.rightButton Attribute:nslayoutattributecenterx relatedby:nslayoutrelationgreaterthanorequal ToItem : Superview attribute:    Nslayoutattributecenterx multiplier:1.0 constant:60.0f]; /*7.    Constraint to position Rightbutton ' s y*/rightbuttonxconstraint.priority = Uilayoutprioritydefaulthigh; Nslayoutconstraint *centerymyconstraint = [Nslayoutconstraint ConstraintWithItem:self.rightButton attribute: Nslayoutattributecentery relatedby:nslayoutrelationgreaterthanorequal Toitem:superview attribute:nslayoutattribut    Ecentery multiplier:1.0f constant:0.0f];   [Superview Addconstraints:@[centerymyconstraint, Rightbuttonxconstraint]]; 8.    Add Text Field Self.textfield = [[Uitextfield alloc]initwithframe:cgrectmake (0, 100, 100, 30)];    Self.textfield.borderStyle = Uitextborderstyleroundedrect;    Self.textfield.translatesAutoresizingMaskIntoConstraints = NO;    [Self.view AddSubview:self.textfield]; 9. Text field Constraints nslayoutconstraint *textfieldtopconstraint = [Nslayoutconstraint ConstraintWithItem:self.tex Tfield Attribute:nslayoutattributetop Relatedby:nslaYoutrelationgreaterthanorequal Toitem:superview attribute:nslayoutattributetop multiplier:1.0 constant:60.0f]; Nslayoutconstraint *textfieldbottomconstraint = [Nslayoutconstraint ConstraintWithItem:self.textfield attribute: Nslayoutattributetop relatedby:nslayoutrelationgreaterthanorequal ToItem:self.rightButton Attribute:nslayoutattri    Butetop multiplier:0.8 constant:-60.0f]; Nslayoutconstraint *textfieldleftconstraint = [Nslayoutconstraint ConstraintWithItem:self.textfield attribute: Nslayoutattributeleft relatedby:nslayoutrelationequal toitem:superview attribute:nslayoutattributeleft multiplier:    1.0 constant:30.0f]; Nslayoutconstraint *textfieldrightconstraint = [Nslayoutconstraint ConstraintWithItem:self.textfield attribute: Nslayoutattributeright relatedby:nslayoutrelationequal Toitem:superview attribute:nslayoutattributeright Multiplie    r:1.0 constant:-30.0f]; [Superview Addconstraints:@[textfieldbottomconstraint, TextfieldleftconstRaint, Textfieldrightconstraint, Textfieldtopconstraint]];    }-(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Output

Run the application and the following output will be shown on the IPhone simulator

When we change the direction of the simulator to the landscape, the output is as follows

When we run the same application on the IPhone 5 emulator, the output is as follows

When we change the direction of the simulator to landscape, the output is as follows:

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.