@interfaceViewcontroller () {Nslayoutconstraint*Yellowviewtopconstraint; Nslayoutconstraint*Blueviewleadconstraint; }@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Uilayoutguide*view_guide =Self.view.layoutMarginsGuide; /** Left Yellow view*/UIView*yellow_view =[[UIView alloc]init]; Yellow_view.backgroundcolor=[Uicolor Yellowcolor]; Yellow_view.translatesautoresizingmaskintoconstraints=NO; [Self.view Addsubview:yellow_view]; //left margin constraintNslayoutconstraint *yellowview_leading = [Yellow_view.leadinganchor constraintequaltoanchor:view_ GUIDE.LEADINGANCHOR constant:Ten]; //Top ConstraintNslayoutconstraint *yellowview_top = [Yellow_view.topanchor ConstraintEqualToAnchor:view_Guide.topAnchor constant: -]; //width ConstraintsNslayoutconstraint *yellowview_width = [Yellow_view.widthanchor constraintequaltoconstant: -]; //Height ConstraintsNslayoutconstraint *yellow_height = [Yellow_view.heightanchor constraintequaltoconstant: -]; [Nslayoutconstraint Activateconstraints:@[yellowview_leading,yellowview_top,yellow_height,yellowview_width]]; Yellowviewtopconstraint=Yellowview_top; /** Center of Red View*/UIView*middleview =[[UIView alloc]init]; Middleview.backgroundcolor=[Uicolor Redcolor]; Middleview.translatesautoresizingmaskintoconstraints=NO; [Self.view Addsubview:middleview]; //Center HorizontallyNslayoutconstraint *middleview_centerx =[Middleview.centerxanchor ConstraintEqualToAnchor:view_Guide.centerXAnchor]; //Center VerticallyNslayoutconstraint *middleview_centery =[Middleview.centeryanchor ConstraintEqualToAnchor:view_Guide.centerYAnchor]; //width ConstraintsNslayoutconstraint *middleview_width = [Middleview.widthanchor constraintequaltoconstant: -]; //Height ConstraintsNslayoutconstraint *middleview_height = [Middleview.heightanchor constraintequaltoconstant: -]; [Nslayoutconstraint Activateconstraints:@[middleview_centerx,middleview_centery,middleview_height,middleview_ Width]]; /** * Create a blue view with a yellow view 10*/UIView*blueview =[[UIView alloc]init]; Blueview.backgroundcolor=[Uicolor Bluecolor]; Blueview.translatesautoresizingmaskintoconstraints=NO; [Self.view Addsubview:blueview]; //left constraintNslayoutconstraint *blueview_leading = [Blueview.leadinganchor constraintEqualToAnchor:yellow_View.trailingAnchor constantTen]; //top constrain to top of yellow view alignNslayoutconstraint *blueview_top =[Blueview.topanchor ConstraintEqualToAnchor:yellow_View.topAnchor]; //width ConstraintsNslayoutconstraint *blueview_width = [Blueview.widthanchor constraintequaltoconstant: -]; //Height ConstraintsNslayoutconstraint *blueview_height = [Blueview.heightanchor constraintequaltoconstant: -]; [Nslayoutconstraint Activateconstraints:@[blueview_leading,blueview_top,blueview_width,blueview_height]]; Blueviewleadconstraint=blueview_leading; //Create an Execute animation buttonUIButton*btnanimate =[UIButton Buttonwithtype:uibuttontypecustom]; [Btnanimate Setbackgroundcolor:[uicolor Redcolor]; [Btnanimate Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Btnanimate settitle:@"Performing animations"Forstate:uicontrolstatenormal]; [Btnanimate addtarget:self Action: @selector (Btnanimateclick:) forcontrolevents:uicontroleventtouchupinside]; Btnanimate.translatesautoresizingmaskintoconstraints=NO; [Self.view Addsubview:btnanimate]; //Center HorizontallyNslayoutconstraint *btnanimate_centerx =[Btnanimate.centerxanchor ConstraintEqualToAnchor:view_Guide.centerXAnchor]; //Bottom ConstraintNslayoutconstraint *btnanimate_bottom = [Btnanimate.bottomanchor constraintEqualToAnchor:view_Guide.bottomAnchor constant:- -]; //width ConstraintsNslayoutconstraint *btnanimate_width = [Btnanimate.widthanchor constraintequaltoconstant: the]; //Height ConstraintsNslayoutconstraint *btnanimate_height = [Btnanimate.heightanchor constraintequaltoconstant: +]; [Nslayoutconstraint Activateconstraints:@[btnanimate_height,btnanimate_width,btnanimate_centerx,btnanimate_ Bottom]]; }- (void) Btnanimateclick: (UIButton *) sender{[UIView animatekeyframeswithduration:1Delay0Options:uiviewkeyframeanimationoptionrepeat animations:^{yellowviewtopconstraint.constant+=Ten; Blueviewleadconstraint.constant+=Ten; [Self.view layoutifneeded]; } Completion:^(BOOL finished) {}]; }
Ios9-nslayoutanchor and uilayoutguide for automatic layout