#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