about iOS layout auto IPhone6 After that is autolayout,autolayout is very useful, but sometimes we need to manually page layout, the VFL is a choice, if the VFL is not very familiar with reference to iOS Development-VFL (Visual format language) and AutoLayout. The VFL is not complex, it is easy to understand, the actual development used in the special cooked well, if the first time to see an estimate to take a bit of effort to get it done. Masonry is a simplified version of the VFL, with more people, before the project used once, for the development of manual writing page is considered a benefit.
Basic Knowledge
First, let's look at a common problem to place a sub-view in the Uiviewcontroller, by setting the margin to achieve the following effect:
If through the VFL our code would be like this:
UIView *superview = Self.view; UIView *view1 = [[UIView alloc] init]; View1.translatesautoresizingmaskintoconstraints = NO; View1.backgroundcolor = [Uicolor Redcolor]; [Superview Addsubview:view1]; Uiedgeinsets padding = uiedgeinsetsmake (200, 50, 200, 50); [Superview addconstraints:@[//constraint [Nslayoutconstraint constr Aintwithitem:view1 Attribute:nslayoutattributetop Relatedby:nslayoutrelationequal Toitem:superview Attribute:nslayoutattrib Utetop multiplier:1.0 Constant:padding.top], [Nslayoutconstraint Constraintwithitem:vie W1 Attribute:nslayoutattributeleft Relatedby:nslayoutrelationequal Toitem:superview Attribute:nslayoutattributeleft multiplier:1.0 Constant:padding.left], [Nslayoutconstraint constraintwithitem:view1 Attribute:nslayoutattributebottom Relatedby:nslayoutrelationequal Toitem:supe Rview Attribute:nslayoutattributebottom multiplier:1.0 Constant:-padding.bottom], [Nslayoutconstraint Constraintwithitem:view1 Attribute:nslayoutattributeright Relatedby:nslayoutrel Ationequal Toitem:superview Attribute:nslayoutattributeright m Ultiplier:1 Constant:-padding.right], ]];
Simply set a margin, if the view of the relationship is more complex, maintenance will be a very painful thing, we look at how masonry is implemented, import the Masonry.h header file, the constraint code:
UIView *childview=[uiview new]; [Childview Setbackgroundcolor:[uicolor Redcolor]; The child view is added to the parent view first [Self.view Addsubview:childview]; __weak typeof (self) weakself = self; Uiedgeinsets padding = uiedgeinsetsmake (+,-); [Childview mas_makeconstraints:^ (Masconstraintmaker *make) { make.edges.equalTo (weakself.view). With.insets (padding); }];
Set the margin through mas_makeconstraints a kind of niaoqianghuanpao feeling, we are about to open a new journey, you can see the following more practical features ~
Practical Knowledge
1.View Setting Size
UIView *childview=[uiview new]; [Childview Setbackgroundcolor:[uicolor Redcolor]; The child view is added to the parent view first [Self.view Addsubview:childview]; __weak typeof (self) weakself = self; [Childview mas_makeconstraints:^ (Masconstraintmaker *make) { //set size make.size.mas_equalTo (Cgsizemake (+)); Center Make.center.equalTo (Weakself.view); }];
The effect is as follows:
Here friendship is actually a small content, currently we set constraints are used by mas_makeconstraints to create constraints, mas_updateconstraints to update constraints, mas_remakeconstraints reset constraints, Clear the previous constraints, keep the latest constraints, if you want to explain in depth, you can read the following English explanation ~
/** * Creates a masconstraintmaker with the callee view. * Any constraints defined is added to the view or the appropriate Superview once the block has finished executing * * @ param block scope within which you can build up the constraints which your wish to apply to the view. * * @return Array of created masconstraints */-(Nsarray *) Mas_makeconstraints: (void (^) (Masconstraintmaker *make)) block; /** * Creates a masconstraintmaker with the callee view. * Any constraints defined is added to the view or the appropriate Superview once the block has finished executing. * If an existing constraint exists then it'll be updated instead. * * @param block scope within which you can build up the constraints which your wish to apply to the view. * * @return Array of created/updated masconstraints */-(Nsarray *) Mas_updateconstraints: (void (^) (Masconstraintmaker *ma KE)) block;/** * Creates a masconstraintmaker with the callee view. * Any constraints defined is added to the view or the APPropriate Superview once the block has finished executing. * All constraints previously installed for the view would be removed. * * @param block scope within which you can build up the constraints which your wish to apply to the view. * * @return Array of created/updated masconstraints */-(Nsarray *) Mas_remakeconstraints: (void (^) (Masconstraintmaker *ma KE)) block;
2. Set the height, here set the left and right margin, so do not set the width, if you want to set the width of the height can refer to the setting method:
UIView *childview=[uiview new]; [Childview Setbackgroundcolor:[uicolor Greencolor]; The child view is added to the parent view first [Self.view Addsubview:childview]; __weak typeof (self) weakself = self; [Childview mas_makeconstraints:^ (Masconstraintmaker *make) { //distance from top of make.top.equalTo (weakSelf.view.mas_top). With.offset (); Distance to left of Make.left.equalTo (weakSelf.view.mas_left). With.offset (+); Distance to the right 30, note is negative make.right.equalTo (weakSelf.view.mas_right). With.offset ( -30); Height make.height.mas_equalTo (@150); }];
3. Position settings between sub-views:
UIView *childview=[uiview New]; [Childview Setbackgroundcolor:[uicolor Greencolor]; The child view is added to the parent view first [Self.view Addsubview:childview]; __weak typeof (self) weakself = self; [Childview mas_makeconstraints:^ (Masconstraintmaker *make) {//distance from top of make.top.equalTo (WeakSelf.view.mas_top). With.offset (44); Distance to left of Make.left.equalTo (WeakSelf.view.mas_left). With.offset (30); Distance to right 30, note is negative make.right.equalTo (weakSelf.view.mas_right). With.offset (-30); Height of make.height.mas_equalTo (@150); }]; Address: Http://www.cnblogs.com/xiaofeixiang/UIView *nextview=[uiview new]; [NextView Setbackgroundcolor:[uicolor Redcolor]; [Self.view Addsubview:nextview]; [NextView mas_makeconstraints:^ (Masconstraintmaker *make) {Make.top.equalTo (Childview.mas_bottom). With.offset (30); Make.right.equalTo (childview.mas_right). With.offset (-30); Make.width.mas_equalTo (@100); Make.height.mas_equalTo (@100); }];
4. Chained notation is a handy formula:
UIView *childview=[uiview New]; [Childview Setbackgroundcolor:[uicolor Greencolor]; The child view is added to the parent view first [Self.view Addsubview:childview]; __weak typeof (self) weakself = self; [Childview mas_makeconstraints:^ (Masconstraintmaker *make) {Make.top.and.left.mas_equalTo (Weakself.view). With.offset (100); Make.bottom.and.right.mas_equalTo (Weakself.view). With.offset (-100); The second is simpler, relative to the parent view//make.top.and.left.mas_equalTo (+);//Make.bottom.and.right.mas_equalTo (-100); }]; UILabel *label=[uilabel New]; [Label settext:@ "blog Park-flyelephant"]; [Label Settextcolor:[uicolor Redcolor]; [Label Settextalignment:nstextalignmentcenter]; [Self.view Addsubview:label]; [Label mas_makeconstraints:^ (Masconstraintmaker *make) {Make.left.mas_equalTo (Weakself.view). With.offset (10); Make.height.mas_equalTo (20); Make.right.mas_equalTo (Weakself.view). With.offset (-10); Make.bottom.mas_equalTo (Weakself.view). WITh.offset (-50); }];
Online about masonry A lot of tutorials, give examples of many, these situations basically meet the needs of the development, there will not be too much in and out, is a simple version of the tutorial, masonry in the properties and iOS properties are related to the relationship, but because it is very simple, basically not how to see, is a control relationship:
iOS Development-masonry Easy Tutorial