Use the Constraintwithitem, Constraintswithvisualformat, two class methods in the Nslayoutconstraint class of AutoLayout to create a view and enable automatic layout

Source: Internet
Author: User

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload{[Super Viewdidload];    [Self createviewwithconstraintitem]; [Self createviewwithconstraint];}- (void) createviewwithconstraintitem{UIView*redview =[[UIView alloc]init]; Redview.backgroundcolor=[Uicolor Redcolor]; Redview.translatesautoresizingmaskintoconstraints=NO;        [Self.view Addsubview:redview]; /** Left margin*/Nslayoutconstraint*viewleftconstraint =[Nslayoutconstraint Constraintwithitem:redview attribute:n                                                            Slayoutattributeleading relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributeleading Multiplier:1constant -. f]; /** Top Margin*/Nslayoutconstraint*viewtopconstraint =[Nslayoutconstraint Constraintwithitem:redview Attribute:nslayouta Ttributetop relatedby:nslayoutrelationequal ToItem:self.view at Tribute:nslayoutattributetop Multiplier:1constant -. f]; /*--------------------------------------------------------------------------------Note: It is usually better not to fix the height of the width, In addition, when Viw1.attribute is not equal to View2.attribute*multiplier +constant, We want to set the Toitem in the Constraintwithitem function to nil and the attribute parameter to Nslayoutattributenotanattribute------------------------- --------------------------------------------------------*/    /** Width*/Nslayoutconstraint*viewwidthconstaint =[Nslayoutconstraint Constraintwithitem:redview attribute:n                                                            Slayoutattributewidth relatedby:nslayoutrelationgreaterthanorequal Toitem:nil A Ttribute:nslayoutattributenotanattribute Multiplier:1constant Max]; /** Height*/Nslayoutconstraint*viewheightconstaint =[Nslayoutconstraint Constraintwithitem:redview Attribu                                                                 Te:nslayoutattributeheight relatedby:nslayoutrelationgreaterthanorequal                                                        Toitem:nil Attribute:nslayoutattributenotanattribute Multiplier:1constant Max]; [Self.view addconstraints:@[viewleftconstraint,viewtopconstraint,viewwidthconstaint,viewheightconstaint];}- (void) createviewwithconstraint{/** Create left View*/UIView*leftview =[[UIView alloc]init]; Leftview.backgroundcolor=[Uicolor Redcolor]; Leftview.translatesautoresizingmaskintoconstraints=NO;        [Self.view Addsubview:leftview]; /** Create right view*/UIView*rightview =[[UIView alloc]init]; Rightview.backgroundcolor=[Uicolor Bluecolor]; Rightview.translatesautoresizingmaskintoconstraints=NO;            [Self.view Addsubview:rightview]; /** Create bottom right view*/UIView*rightbottomview =[[UIView alloc]init]; Rightbottomview.backgroundcolor=[Uicolor Yellowcolor]; Rightbottomview.translatesautoresizingmaskintoconstraints=NO;        [Self.view Addsubview:rightbottomview]; Nsdictionary*viewdic =nsdictionaryofvariablebindings (Leftview,rightview,rightbottomview); /*The left margin of the parent view is 50 and its width is greater than or equal to*/Nsarray*leftview_h = [Nslayoutconstraint constraintswithvisualformat:@"H:|-50-[leftview (>=150)]"Options0Metrics:nil Views:viewdic]; /*from parent view top margin 100 and self height greater than or equal to*/Nsarray*leftview_v = [Nslayoutconstraint constraintswithvisualformat:@"V:|-100-[leftview (>=150)]"Options0Metrics:nil Views:viewdic]; /*horizontal layout, Rightview to the right of Leftview standard distance, and the width is not less than 50 points. */Nsarray*rightview_h = [Nslayoutconstraint constraintswithvisualformat:@"H:[leftview]-[rightview (>=50)]"Options0Metrics:nil Views:viewdic]; Nsarray*rightview_v = [Nslayoutconstraint constraintswithvisualformat:@"V:|-100-[rightview (>=50)]"Options0Metrics:nil Views:viewdic]; Nsarray*rightbottomview_h = [Nslayoutconstraint constraintswithvisualformat:@"H:[leftview]-[rightbottomview (>=50)]"Options0Metrics:nil Views:viewdic]; /*The vertical layout is a distance from the parent view 100, and the other rightbottomview below the Rightview is the equivalent of clinging*/Nsarray*rightbottomview_v = [Nslayoutconstraint constraintswithvisualformat:@"V:|-100-[rightview (>=50)][rightbottomview (>=100)]"Options0Metrics:nil Views:viewdic];    [Self.view Addconstraints:leftview_h];        [Self.view Addconstraints:leftview_v];    [Self.view Addconstraints:rightview_h];        [Self.view Addconstraints:rightview_v];    [Self.view Addconstraints:rightbottomview_h];    [Self.view Addconstraints:rightbottomview_v]; }@end

Use the Constraintwithitem, Constraintswithvisualformat, two class methods in the Nslayoutconstraint class of AutoLayout to create a view and enable 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.