Ios9-nslayoutanchor and uilayoutguide for automatic layout

Source: Internet
Author: User

@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

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.