1 //2 //VIEWCONTROLLER.M3 //Ios_0115_autolayout4 //5 //Created by Ma C on 16/1/15.6 //Copyright (c) 2016 blog technology. All rights reserved.7 //8 9 #import "ViewController.h"Ten One @interfaceViewcontroller () A -@property (nonatomic, strong) Nslayoutconstraint *Changebluetop; -@property (nonatomic, strong) UIView *Blueview; the - - - @end + - @implementationViewcontroller + A- (void) Viewdidload { at [Super Viewdidload]; - - //Create a blue view -UIView *blueview =[[UIView alloc] init]; -Blueview.frame = CGRectMake (0,0, -, -); -Blueview.backgroundcolor =[Uicolor Bluecolor]; in [Self.view Addsubview:blueview]; -Self.blueview =Blueview; to //Create a red view +UIView *redview =[[UIView alloc] init]; -Redview.frame = CGRectMake ( -, -, -, -); theRedview.backgroundcolor =[Uicolor Redcolor]; * [Blueview Addsubview:redview]; $ Panax Notoginseng //Disable autoresizing -Blueview.translatesautoresizingmaskintoconstraints =NO; theRedview.translatesautoresizingmaskintoconstraints =NO; + A //Create and add constraints the + //1. Create a constraint for a blue view - $ /* $ ( Nslayoutattribute) property of the (ID) object - (nslayoutrelation) Relationship (> = <) - ( Nslayoutattribute) property of the (ID) object the multiplied by the parameters of the multiplier plus the constant parameters - */Wuyi the //Constraint of Height -Nslayoutconstraint *blueheight = [Nslayoutconstraint constraintwithitem:blueview attribute:nslayoutattributeheight Relatedby:nslayoutrelationequal Toitem:nil Attribute:nslayoutattributenotanattribute Multiplier:1.0constant -]; Wu //Add a constraint to the control - [Blueview addconstraint:blueheight]; About $ //distance to left -Nslayoutconstraint *blueleft = [Nslayoutconstraint constraintwithitem:blueview attribute:nslayoutattributeleft Relatedby:nslayoutrelationequal ToItem:blueView.superview Attribute:nslayoutattributeleft Multiplier:1.0constant -]; - [Blueview.superview Addconstraint:blueleft]; - A //distance above (toplayoutguide status bar) +Nslayoutconstraint *bluetop = [Nslayoutconstraint constraintwithitem:blueview attribute:nslayoutattributetop Relatedby:nslayoutrelationequal ToItem:blueView.superview attribute:nslayoutattributetop Multiplier:1.0constant -]; the [Blueview.superview addconstraint:bluetop]; -Self.changebluetop =Bluetop; $ the the //from the right . theNslayoutconstraint *blueright = [Nslayoutconstraint constraintwithitem:blueview attribute:nslayoutattributeright Relatedby:nslayoutrelationequal ToItem:blueView.superview attribute:nslayoutattributeright Multiplier:1.0constant:- -]; the [Blueview.superview addconstraint:blueright]; - in //1. Create a constraint on a red view the //the height of the red view is equal to the height of the blue view theNslayoutconstraint *redheight = [Nslayoutconstraint constraintwithitem:redview attribute:nslayoutattributeheight Relatedby:nslayoutrelationequal Toitem:blueview attribute:nslayoutattributeheight Multiplier:1.0constant0]; About [Redview.superview addconstraint:redheight]; the the //Red View top distance blue View Bottom30 theNslayoutconstraint *redtop = [Nslayoutconstraint constraintwithitem:redview attribute:nslayoutattributetop RelatedBy : Nslayoutrelationequal toitem:blueview Attribute:nslayoutattributebottom multiplier:1.0constant -]; + [Redview.superview addconstraint:redtop]; - the //Red View right-aligned with blue viewBayiNslayoutconstraint *redright = [Nslayoutconstraint constraintwithitem:redview attribute:nslayoutattributeright Relatedby:nslayoutrelationequal Toitem:blueview attribute:nslayoutattributeright Multiplier:1.0constant0]; the [Redview.superview addconstraint:redright]; the - //the width of the red view is equal to half the width of the blue view -Nslayoutconstraint *redwidth = [Nslayoutconstraint constraintwithitem:redview attribute:nslayoutattributewidth Relatedby:nslayoutrelationequal Toitem:blueview attribute:nslayoutattributewidth Multiplier:0.5constant0]; the [Redview.superview addconstraint:redwidth]; the the //modifying constraints for animating effects the -UIButton *btn =[UIButton Buttonwithtype:uibuttontypecustom]; the the[Btn Setframe:cgrectmake (0, -, -, -)]; the[BTN Settitle:@"Moving"Forstate:uicontrolstatenormal];94 [btn Setbackgroundcolor:[uicolor orangecolor]; the [Self.view addsubview:btn]; the [btn addtarget:self Action: @selector (Btnclick) forcontrolevents:uicontroleventtouchupinside]; the 98 } About -- (void) Btnclick101 {102 //Modify Constraint-no frame of blue view is computed based on the new constraint103Self.changeBlueTop.constant + = -;104[UIView animatewithduration:1animations:^{ the //Modify the new frame according to the new constraint106 [Self.blueview layoutifneeded];107 }];108 }109 the 111- (void) didreceivememorywarning { the [Super didreceivememorywarning];113 //Dispose of any resources the can be recreated. the } the the @end
IOS ui-Auto Layout (AutoLayout)