"Original" iOS learning masonry third party constraints

Source: Internet
Author: User

1. Masonry overview

    • Currently the most popular AutoLayout third-party framework

Write AutoLayout in an elegant code way

Omit Apple's official disgusting AutoLayout code.

Greatly improved the development efficiency

    • Frame address:https://github.com/SnapKit/Masonry

2. Common methods

    • This method will only add new constraints.
[Blueview mas_makeconstraints:^ (Masconstraintmaker * make) {}];
    • This method will delete all previous constraints and add new constraints     
[Blueview mas_remakeconstraints:^ (Masconstraintmaker * make) {       }];
    • This method will overwrite some of the previous specific constraints      
[Blueview mas_updateconstraints:^ (Masconstraintmaker * make) {       }];  

3. Constraint type

    • Size:

Width (width) \height (h) \size (size)

        // width Constraints        Make.width.mas_equalTo (+);         // height Constraints        Make.height.mas_equalTo (+);                 //   size constraint (equivalent to the above two sentences)        make.size.mas_equalTo (cgsizemake());
    • Boundary:

Left\leading (left border) \right\trailing (right boundary) \top (top boundary) \bottom (bottom boundary)  

       // left (leading similar)        Make.left.mas_equalTo (Self.view). Offset (+);         // right (trailing similar)        Make.right.equalTo (Self.view). Offset (-);         // Top of        Make.top.equalTo (Self.view). Offset (+);         // Bottom        Make.bottom.mas_equalTo (Self.view). Offset (-);
    • Center point:

Center\centerx\centery

    // Center (horizontal + vertical)     // size is half    of the parent control [Blueview mas_makeconstraints:^ (Masconstraintmaker * make) {        make.size.mas_equalTo (self.view). Multipliedby (0.5);         // equivalent to the following two lines of code //         Make.centerX.mas_equalTo (self.view); //         Make.centerY.mas_equalTo (self.view);     }];
    • The inner margin implements the boundary constraint:

Edges

// uiedgeinsets inner margin Make.edges.mas_equalTo (Self.view). insets (uiedgeinsetsmake(50 ));

4. The difference between MAS_ prefix modification and non-modification    

    • Mas_equalto and Equalto

By default:

Mas_equalto has automatic packaging function, such as automatic packaging of 20 for the @20

Equalto No automatic packaging function

  Mas_equalto is more powerful than > Equalto, and can always be used Mas_equalto

    • Mas_width and Width

By default:

Width is a property of the Make object that is used to add a width constraint, which means that the width is constrained

Mas_width is a property value that is used as a Equalto parameter to represent the width property of a control

Mas_height, Mas_centerx, etc.

    • Elimination of the differential approach

If you add the following macro, then Mas_equalto and Equalto are no different

#define Mas_shorthand_globals//Note: This macro must be added to the front of #import "Masonry.h"

If you add the following macro, Mas_width can also be written as width

#define Mas_shorthand

//Define this constant if you want to use masonry without the ' mas_ ' prefix#defineMas_shorthand//Define this constant if your want to enable auto-boxing for default syntax#defineMas_shorthand_globals#import "Masonry.h"- (void) viewdidload {[Super viewdidload]; //Blue ControlsUIView *blueview =[[UIView alloc] init]; Blueview.backgroundcolor=[Uicolor Bluecolor];       [Self.view Addsubview:blueview]; //Red ControlsUIView *redview =[[UIView alloc] init]; Redview.backgroundcolor=[Uicolor Redcolor];       [Self.view Addsubview:redview]; //Add constraintCGFloat margin = -; CGFloat height= -; [Blueview makeconstraints:^ (Masconstraintmaker *Make )        {Make.left.equalTo (self.view.left). offset (margin); Make.right.equalTo (redview.left). Offset (-margin); Make.bottom.equalTo (Self.view.bottom). Offset (-margin);        Make.height.equalTo (height);        Make.top.equalTo (Redview.top);        Make.bottom.equalTo (Redview.bottom);    Make.width.equalTo (Redview.width);       }]; [Redview makeconstraints:^ (Masconstraintmaker *Make ) {Make.right.equalTo (self.view.right). Offset (-margin); }];} 

5, the use of dispensable

The following methods are only for readability, optional

    • With
-(masconstraint*) with {    return self ;}
Usage Sample Code
//size limit: 100x100//position: The bottom right corner of the parent control is glued, the spacing is[Blueview mas_makeconstraints:^ (Masconstraintmaker *Make ) {//width ConstraintsMake.width.equalTo (@ -); //Height ConstraintsMake.height.equalTo (@ -); //RightMake.right.equalTo (self.view.mas_right). With.offset (- -); //Top ofMake.top.equalTo (self.view.mas_top). With.offset ( -); }];
    • and
-(masconstraint*) and {    return self ;}

Usage Sample Code

 //  //  position: adhere to the lower-right corner of the parent control, spacing is   [Blueview Mas_ Makeconstraints:  ^ (masconstraintmaker *make) { //  make.width.and.height.mas_equalTo (100  );  //  right  Make.right.equalTo (        Self.view). Offset (-20   //  top  Make.top.equalTo (            Self.view). Offset (20  

"Original" iOS learning masonry third party constraints

Related Article

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.