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.} @endOutput
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: