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
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());
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\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
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
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
-(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 ( -); }];
-(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