Automatic layout for iOS

Source: Internet
Author: User

An introduction to iOS auto-layout one, storyboard and xib before that, let's briefly introduce Xib/nib and storyboard. Xib/nib can actually be considered to be the same, NIB is the product of the iOS3.0 before, its essence is a folder with executable binary files. The essence of Xib is an XML-type profile that enables visual programming. Both are converted to an executable nib file before the Uiviewcontroller life cycle method Loadview method.               Storyboard is a description of the collection of multiple Xib, which is also in XML format. The difference between               storyboard and Xib is that A project can have more than one xib, a xib corresponding to a controller and multiple views; a project requires only one storyboard, and it is preferable to use storyboard when multiple controllers are included. And storyboard can clearly describe the navigation relationship between interfaces.   Analysis of the advantages and disadvantages of pure code, storyboard and Xib coding                UI processing is roughly divided into pure code, storyboard, and xib three ways, so here we compare the pros and cons of three ways. l  Pure Code Pure code is increasingly respected by coder, pure code implementation of the UI with the best code reuse, but also take advantage of version management, code tracking and code consolidation, large-scale project use. However, the problem of pure code implementation is long development cycle, complex code maintenance and automatic layout AutoLayout difficult l  StoryBoard (iOS5.0) StoryBoard implementation of the advantages of the main highlights in the development cycle is short, the code is small, You can clearly see the layout style of each viewcontroller and the jump relationship between the individual controllers.               of course its drawbacks are obvious, not suitable for multi-person collaborative development, Viewcontroller reuse and custom view implementation l  Xibxib has the advantage that the development cycle is fast and the version maintenance and pure code differences are not small, easy to maintain.               its disadvantage is often reflected in the xib setting is often not the final setting, will eventually be replaced by pure code. Xib Use Note: Try to isolate the work of xib from the work of the code: You can use Xib to do things using xib, for example: three labels where two fonts are set in Xib and the other is done in the code. It would be a good way to try to keep only the necessary, less iboutlet and ibaction.   Automatic layout of three or three types of pure code constraints automatic layouts after three periods magicnumber -> autoresizingmask -> autolayout  In the IPHONE1-IPHONE3GS era the size of window is fixed to (320,480) we just need to calculate the relative position simply. In the iphone4-iphone4s era, Apple launched the Retina screen   But the size of window does not change in the Iphone5-iphone5s Era window size changed (320,568) then autoresizingmask  AutoLayout Automatic layout is introduced after the iOS6.0, before 6.0 using the Autoresizingmasks property, the enumeration type of properties are flexible (scaling), after the AutoLayout this is rarely used, can not be understood. Storyboard and Xib set constraints here also do not do a specific introduction, the main introduction of three kinds of handwritten code to set the constraints of the method. 1, using the system Nslayoutconstraint implementation obj1.property1 = (Obj2.property2 * multiplier) + constant Value multiplier and constant is the vector coefficients and offsets 1) one set constraint-(void) AddConstraint: (Nslayoutconstraint *) Constraint;[self.view addconstraint:[nslayOutconstraint constraintwithitem:_testview1 attribute:nslayoutattributetop relatedby: nslayoutrelationequal toitem:self.view attribute:nslayoutattributetop multiplier:1.0  CONSTANT:30]];2)   A group set of Constraints  -(void) addconstraints: (Nsarray *) constraints;[self.view addconstraints :@[                                //Up                                   [Nslayoutconstraint constraintwithitem:_ testview1 attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.view  attribute:nslayoutattributetop multiplier:1.0 constant:30],                                //Left                                  [nslayoutconstraint  constraintwithitem:_testview1 attribute:nslayoutattributeleft relatedby:nslayoutrelationequal  toitem:self.view attribute:nslayoutattributeleft multiplier:1.0 constant:10],                                 //Down                                   [Nslayoutconstraint constraintwithitem:_testview1 attribute: nslayoutattributebottom Relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributebottom multiplier :1.0 constant:-170],                                //Wide                                  [nslayoutconstraint  constraintwithitem:_testview1 attribute:nslayoutattributewidth relatedby:nslayoutrelationequal  TOITEM:SELF.VIEW&NBSP;ATTRIBUTE:NSLAYOUTATTRIBUTEWIDTH&NBSP;MULTIPLIER:0.45&NBSP;CONSTANT:0]]];2)  Visual Format Language, translated by "Visual format language"   is the abstract language that Apple introduced in order to simplify AutoLayout encoding   equivalent to play hieroglyphs, and to write all in the string, encountering errors more difficult to adjust, But relatively speaking, the code is much leaner. The detailed syntax of the VFL   can be found in the official documentation   "Visual Format" can be searched in   The above method to upload the two parameters metrics and views are two dictionaries. The first dictionary metrics is used to tell each value in the system which letter corresponds to the second one.The dictionary views are   used to tell each of the controls inside the corresponding string what if all the controls in the entire VFL language are written in the original, you can use a macro instead of   Comment:   |: Represents the parent view  -: Represents the distance  V: : Represents vertical  H: : Represents horizontal  >=: Indicates that the view spacing, width, and height must be greater than or equal to a value  <=: Indicates that the view spacing, width, and height must be small or equal to a value  ==: Indicates that the view spacing, width, or height must be equal to a value  @ : >=, <=, ==  limit    max   1000   1.| The-[view]-|:  view is within the left and right edges of the parent view  2.| -[view] :   View is at the left edge of the parent view  3.| [view]  :   View and parent view left align  4.-[view]- :  set the width height of the view  5.| -30.0-[view]-30.0-|:  represents the parent view   left and right spacing   30 6. [View (200.0)]: Indicates that the view width is 200.0 7.| -[view (View1)]-[view1]-| : Represents the width of the view and  8 within the left and right edges of the parent view. V:|-[view (50.0)]: View height is   50 9:v:|-(==padding)-[imageview]->=0-[button]-(==padding)-| : Represents the distance from the parent view the two view spacing must be greater than or equal to 0 and the distance from the bottom parent view is padding.  10:  [Wideview (>[email protected])] : The width of the view is at least 60 and cannot exceed   700&NBSP;11: If no declaration direction defaults to   Horizontal   h: -(void) SETCONSTRAINTSBYVFL {    nsdictiOnary *views = nsdictionaryofvariablebindings (SELF.VIEW,_TESTVIEW1,_TESTVIEW2,_TESTVIEW3);        [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@] H:|-10 -[_testview1 (==_TESTVIEW2)]-10-[_testview2]-10-| "  options:0 metrics:0 views:views]];    [self.view addconstraints:[ nslayoutconstraint constraintswithvisualformat:@ "V:|-30-[_testview1 (<=700)]-10-[_testView3]-10-|"  options:0 metrics:0 views:views]];    [self.view addconstraints:[ nslayoutconstraint constraintswithvisualformat:@ "V:|-30-[_testview2 (<=700)]-10-[_testView3]-10-|"  options:0 metrics:0 views:views]];    [self.view addconstraints:[ nslayoutconstraint constraintswithvisualformat:@ "|-10-[_testview3]-10-|"  options:0 metrics:0 views:views]];    [self.view addconstraints:[ Nslayoutconstraint constraintswithvisualformat:@ "V:[_TESTVIEW3 (==150)]-10-|" Use of  options:0 metrics:0 views:views]];   } 2.masonry mas_makeconstraints is to add constraints to the view, there are several constraints, respectively, is the margin, width, height, left upper right lower distance, baseline. After the constraint has been added, there can be corrections, fixed offset (displacement) correction and multipliedby (magnification) correction. The syntax is generally make.equalto or make.greaterthanorequalto or Make.lessthanorequalto + multiple and displacement corrections.   Note 1:  elements that use the Mas_makeconstraints method must be added to the parent element beforehand, such as [Self.view addsubview:view];  note 2:masequalto and Equalto difference: Masequalto is more than Equalto type conversion operation, generally speaking, most of the time two methods are   common, but for numeric elements use Mas_equalto. For the handling of objects or multiple properties, use Equalto. In particular, multiple attributes must be used, such as Make.left.and.right.equalTo (Self.view);  Note Point 3: Notice the method with and and, and this method does not actually do any action, Equalto The method simply returns the object itself, and this method is left and right for the readability of the method when it is written  . Make.left.and.right.equalTo (Self.view), and Make.left.right.equalTo (Self.view), are exactly the same, but the obvious addition of the and method of the statement is more readable. The  exp1:  center point is the same as Self.view, with a width of 400*400-(void) exp1{    uiview *view =  [uiview new];    [view setbackgroundcolor:[uicolor redcolor]];    [self.view addsubview:view];    [view mas_makeconstraints:^ ( Masconstraintmaker *make)  {         make.center.equalto (Self.view);          make.size.mas_equalto (CGSizeMake (400,400));     }];} exp2:  the upper and lower left and right margins are 10-(void) exp2{    uiview *view = [uiview new];     [view setbackgroundcolor:[uicolor redcolor]];    [ self.view addsubview:view];    [view mas_makeconstraints:^ (MASConstraintMaker  *make)  {        make.edges.equalto (Self.view). With.insets ( Uiedgeinsetsmake (10, 10, 10, 10));        //   make.left.equalto (Self.view). With.offset ();         // & NBsP;make.right.equalto (Self.view). With.offset ( -10);        //   make.top.equalto (Self.view). With.offset (Ten);        //   make.bottom.equalto (Self.view). With.offset ( -10);     }];} exp3  two view with a height of 150 vertically centered and equal width and equal interval arrangement   interval 10-(void) exp3{    uiview *view1  = [uiview new];    [view1 setbackgroundcolor:[uicolor redcolor]];     [self.view addsubview:view1];    uiview *view2 =  [UIView new];    [view2 setBackgroundColor:[UIColor redColor]];     [self.view addsubview:view2];    [view1 mas_makeconstraints:^ ( Masconstraintmaker *make)  {        make.centery.mas_equalto ( Self.view.mas_centerY);      &nBsp;  make.height.mas_equalto (        make.width.mas_); Equalto (view2.mas_width);         make.left.mas_equalto (Self.view.mas_ left). With.offset,         make.right.mas_equalto (view2.mas_left). Offset ( -10);     }];    [view2 mas_makeconstraints:^ ( Masconstraintmaker *make)  {        make.centery.mas_equalto ( Self.view.mas_centerY);         make.height.mas_equalto (; )        make.width.mas_equalto (view1.mas_width);         make.left.mas_equalto (view1.mas_right). With.offset (Ten);         make.right.equalto (self.view.mas_right). Offset ( -10);     }];}    

Automatic layout of iOS

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.