Storyboard upgrade-use constraints in your code

Source: Internet
Author: User

We are apart from

Storyboard can constrain the control, in the code we can also constrain it, so that your app no matter what device, his interface design will not change;

////VIEWCONTROLLER.M//Automatic Layout Code edition////Created by Biaoac on 16/3/26.//copyright©2016 year Scsys. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end//using the StoryBoard ID to locate the controller//Uistoryboard *s = [Uistoryboard storyboardwithname:@ "Main" Bundle:[nsbundle Mainbundle]];//Viewcontroller *VC = [s instantiateviewcontrollerwithidentifier:@ "Viewcontroller"];/*//Use Storyboard jump page can also use code to jump//right-drag button to another view controller will pop up: 1.show (push) 2.present (modal) 3.popover (ipad pop-up list) 
    *//*Landscape: 100 from the left side of the parent view, the width of the view itself is 100 hours from the right side of the parent view is 100 vertical: from the top of the parent view is 150, the view itself is a minimum height of 30 using the Visual Format language: Vfl:visual format language Description H: Represents the Horizontal direction, V: Indicates the vertical direction; | : Represents the parent View-: itself represents a distance-distance-represents a distance [string representation of a view] reference view [The view of a string representation (the width of the view or the minimum maximum width, height, and width)] Note; Parentheses do not lose the use of the Code Auto Layout (autorlayout) Frame will fail,--"No need to go to set the frame of the view to use the code automatically layout when you need to disable translatesautoresizingmaskintoconstraints = No; Use automatic layout steps: 1, Transla Tesautoresizingmaskintoconstraints = No; 2, binding View and string 3, adding constraints*/@implementationViewcontroller- (void) viewdidload {[Super viewdidload];//[self demo1];//[self demo2];//[self DEMO3];//[self Demo4];[self demo5];}/** * How to create a view * * @param view needs to create a class name string for the view * @param Sview added to the target parent view * * @return created and added to the views on the parent view*///Here we encapsulate the method of creating views in demo2.3, 4, 5, using-(UIView *) Creatview: (NSString *) View Addtoview: (UIView *) sview{UIView*myview =[[Nsclassfromstring (view) alloc]init]; Myview.translatesautoresizingmaskintoconstraints=NO;    [Sview Addsubview:myview]; returnMyView;}//let's start with the normal process .- (void) demo1{Self.view.backgroundColor=[Uicolor Whitecolor]; UIView*view =[[UIView alloc]init]; View.backgroundcolor=[Uicolor Orangecolor];        [Self.view Addsubview:view]; //Visual Language://1. DisableView.translatesautoresizingmaskintoconstraints =NO; //2. Binding views and StringsNsdictionary *dic =nsdictionaryofvariablebindings (view); //3. Adding Constraints    /** * * * visualformat VFL statement * Options: Based on which direction to layout * metrics: Bound value (NSNumber) and String * Views: binding views and strings.*/[Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"H:|-100-[view (>=100)]-100-|"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; //H:|-100-[view (>=100)]-100-| //V:|-150-[view (+)][Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:|-150-[view (+)]"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; }//then we added a view on the basis of Demo1- (void) demo2{UIView*view1 = [Self Creatview:@"UIView"AddToView:self.view]; View1.backgroundcolor=[Uicolor Redcolor]; UIView*view2 = [Self Creatview:@"UIView"AddToView:self.view]; View2.backgroundcolor=[Uicolor Cyancolor]; //2. BindingNsdictionary *dic =nsdictionaryofvariablebindings (VIEW1,VIEW2); //3. Adding Constraints//View1://h:|-100-[view1 (>=100)]-100-| //V:|-150-[view1 (+)][Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"h:|-100-[view1 (>=100)]-100-|"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; [Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:|-150-[view1 (+)]"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; //View2://h:|-100-[view1 (>=100)]-100-| //v:[view1]-50-[view2 (view1)][Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"h:|-100-[view2 (>=100)]-100-|"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; [Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"v:[view1]-50-[view2 (view1)]"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; }//Research findings: Two views there's a vertical intersection. We did a little bit of optimization .- (void) demo3{UIView*view1 = [Self Creatview:@"UIView"AddToView:self.view]; View1.backgroundcolor=[Uicolor Redcolor]; UIView*view2 = [Self Creatview:@"UIView"AddToView:self.view]; View2.backgroundcolor=[Uicolor Cyancolor]; //2. BindingNsdictionary *dic =nsdictionaryofvariablebindings (VIEW1,VIEW2); //3. Adding Constraints//View1://h:|-100-[view1 (>=100)]-100-| //V:|-150-[view1 (+)][Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"h:|-100-[view1 (>=100)]-100-|"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; //View2://h:|-100-[view1 (>=100)]-100-| //v:[view1]-50-[view2 (view1)][Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"h:|-100-[view2 (>=100)]-100-|"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; [Self.view addconstraints:[nslayoutconstraint Constraintswithvisualformat:@"V:|-150-[view1 (]-50-[VIEW2) (View1)]"options:nslayoutformatalignallleft Metrics:nil Views:dic]]; }//continue to optimize, make the code clearer,- (void) demo4{UIView*view1 = [Self Creatview:@"UIView"AddToView:self.view]; View1.backgroundcolor=[Uicolor Redcolor]; UIView*view2 = [Self Creatview:@"UIView"AddToView:self.view]; View2.backgroundcolor=[Uicolor Cyancolor]; Nsdictionary*dic =nsdictionaryofvariablebindings (VIEW1,VIEW2); ///Array of all VFL statementsNsarray *constriants = @[@"h:|-100-[view1 (>=100)]-100-|",@"h:|-100-[view2 (>=100)]-100-|",@"V:|-150-[view1 (]-50-[VIEW2) (View1)]"];  for(intI=0; i<constriants.count; i++) {[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:constriants[i] Options:NSLayoutFo    Rmatalignallleft Metrics:nil Views:dic]]; }        }//This time we've added the metrics parameter is used to bind arguments and strings, and the binding UIView is the same.- (void) demo5{UIView*view1 = [Self Creatview:@"UIView"AddToView:self.view]; UIView*view2 = [Self Creatview:@"UIView"AddToView:self.view]; UIView*VIEW3 = [Self Creatview:@"UIView"AddToView:self.view]; UIView*VIEW4 = [Self Creatview:@"UIView"AddToView:self.view]; Nsdictionary*dic =nsdictionaryofvariablebindings (VIEW1,VIEW2,VIEW3,VIEW4); NSNumber*leftspace = @ -; NSNumber*rightspace = @ -; NSNumber*view1topspace = @ Max; NSNumber*view1minwidth = @ -; NSNumber*view1hight = @ +; NSNumber*view2topspace = @ -; Nsdictionary*metrics =nsdictionaryofvariablebindings (Leftspace,rightspace,view1minwidth,view1hight,view1topspace,view2topspace)            ; Nsarray*constraints = @[@"h:|-leftspace-[view1 (>=view1minwidth)]-rightspace-|",@"h:|-leftspace-[view2 (view1)]-rightspace-|",@"v:|-view1topspace-[view1 (view1hight)]-view2topspace-[view2 (view1)]"];  for(intI=0; i<constraints.count; i++) {[Self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:constraints[i] Options:NSLayoutFo    Rmatalignallleft Metrics:metrics Views:dic]]; }        }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

I think the code in the comments are very detailed, do not talk, the results of the operation is as follows; (see no, I said no impact)

Storyboard upgrade-use constraints in your code

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.