turn from: http://www.cnblogs.com/wqcoder/p/5511434.html
Brief
The most important thing about automatic layout is constraint: a mathematical expression of the relationship between UI elements. Constraints include dimensions, relative positions that are managed by priority and threshold values. They are additive, which may lead to constraint conflicts and constraints resulting in uncertain layout. Both of these situations produce an exception. before use: AutoLayout the difference between several ways to update setneedslayout: Tell the page that it needs to be updated, but it won't start updating immediately. The layoutsubviews is called immediately after execution. layoutifneeded: Tell the page layout to update immediately. So it is commonly used with setneedslayout. If you want to generate a new frame immediately, you need to call this method, and this general layout animation can be used directly after the layout is updated to allow animations to take effect. Layoutsubviews: System rewrite layout setneedsupdateconstraints: tells you that you need to update the constraint, but not immediately updateconstraintsifneeded: Notify immediate update constraint Updateconstraints: System update constraint use
1. Basic use of mas_makeconstraints: Add constraints mas_updateconstraints: Update constraints, also add new constraints
Mas_remakeconstraints: Pre-reset constraint
The Multipler attribute represents the multiplicative factor of the constraint value as the constraint object, and the Dividedby attribute represents the addition factor of the constraint value as the constraint object, which can be used to set the view's aspect ratio.
When the screen is adapted, it is often necessary to fit a corresponding height according to the width of the screen, this is recommended to use the following constraints in the way to adapt the control
[Self.topview AddSubview:self.topInnerView];
[Self.topinnerview mas_makeconstraints:^ (Masconstraintmaker *make) {
make.height.equalTo (self.topView.mas_height). Dividedby (3);
Make.width.and.height.lessThanOrEqualTo (Self.topview);
Make.width.and.height.equalTo (Self.topview). With.prioritylow ();
Make.center.equalTo (Self.topview);
}];
Prioritylow () sets constraint precedence #define MAS_SHORTHAND_GLOBALS using global macro definitions, you can make Equalto equivalent to Mas_equalto #define MAS_SHORTHAND use global macro definitions, You can not use the Mas_ prefix when calling the masonry method
Here is a place to note that when the global macro definition is used, it is found that there can be a class ' nsarray+masadditions.h ' that can be
self.buttonviews = @[Raisebutton. Lowerbutton, Centerbutton];
You can then in the Updateconstraints method
-(void) updateconstraints {
[self.buttonviews updateconstraints:^ ( Masconstraintmaker *make) {
make.baseline.equalTo (self.mas_centery). With.offset (Self.offset);
[Super updateconstraints];
}
To dynamically modify a View constraint:
Create a View constraint
[Blueview mas_makeconstraints:^ (Masconstraintmaker *make) {
Self.animatableconstraint = Make.edges.equalTo (Superview). insets (Paddinginsets). Prioritylow ();]
;
Change constraint (in another method)
uiedgeinsets paddinginsets = uiedgeinsetsmake (padding, padding, padding, padding);
Self.animatableConstraint.insets = paddinginsets;
[self layoutifneeded];
Debug mode:
Add a key value to a view
Greenview.mas_key = @ "Greenview";
or in the following order
Masattachkeys (Greenview, Redview, Blueview, Superview);
The same for each constraint can be added key
make.height.greaterThanOrEqualTo (@5000). Key (@ "Constantconstraint");
Preferredmaxlayoutwidth: A constraint problem with multiline label
Confirmed the location
//In layoutsubviews confirm the preferredmaxlayoutwidth value of the label
-(void) layoutsubviews {
[super Layoutsubviews];
You must set the Preferredmaxlayoutwidth
after the [super Layoutsubviews] Call, after the Longlabel frame has a value Self.longLabel.preferredMaxLayoutWidth = self.frame.size.width-100;
After you set up preferredlayoutwidth, you need to rearrange
[Super layoutsubviews];
}
ScrollView use constraints: The principle is to constrain the contentsize size of the ScrollView by a contentview, which means the size of the parent view is controlled by the child control's constraints
1. Control ScrollView Size (display area)
[Self.scrollview makeconstraints:^ (Masconstraintmaker *make) {
Make.edges.equalTo ( Self.view);
}];
2. Add a Contentview to ScrollView and add the constraint
[Contentview makeconstraints:^ (Masconstraintmaker *make) {
Make.edges.equalTo (Self.scrollview);
Note the width constraint here, which is constrained by the addition of the item
make.width.equalTo (Self.scrollview);
3. Contentview control of the child controls to achieve the control of the size of the Contentview
New method: 2 or more controls equal interval sort
/**
* multiple control fixed interval arrangement, varying the length of the control or width of the value
* * @param axistype Axis direction
* @param fixedspacing interval Size
* @param leadspacing head interval
* @param tailspacing Trailing interval
* *
-(void) Mas_distributeviewsalongaxis: (masaxistype) Axistype
withfixedspacing: (cgfloat) fixedspacing l
eadspacing: (cgfloat) leadspacing
tailspacing: (cgfloat) tailspacing;
/**
* the equal spacing arrangement of multiple fixed sized controls, varying the spacing gap * * @param axistype Axis direction
* @param Fixeditemlength The fixed length or width value of each control
* @param leadspacing head interval
* @param tailspacing trailing interval
*/
-(void) Mas_distributeviewsalongaxis: (masaxistype) Axistype
withfixeditemlength: (cgfloat) Fixeditemlength
leadspacing: (cgfloat) leadspacing
tailspacing: (cgfloat) tailspacing;
The use of the method is simple because it is a nsarray class extension:
Create a horizontal arrangement icon arr 2 or more initialized controls
// Alongaxis axis fixed interval head interval trailing interval
[arr mas_ Distributeviewsalongaxis:masaxistypehorizontal withfixedspacing:20 leadspacing:5 TailSpacing:5];
[Arr makeconstraints:^ (Masconstraintmaker *make) {
make.top.equalTo (@60);
Make.height.equalTo (@60);
}];
2. Note Constraint The View object can be added to the view only after it has been addsubview when all your constraints are called within updateconstraints, you need to call this method here because the Updateconstraints method needs to be triggered
Call inside view, not Viewcontroller
+ (BOOL) requiresconstraintbasedlayout {return
YES;
}
/**
* Apple recommended constraint additions and modifications placed in this method type/
-(void) updateconstraints {
[Self.growingbutton updateconstraints : ^ (Masconstraintmaker *make) {
make.center.equalTo (self);
Make.width.equalTo (@ (self.buttonSize.width)). Prioritylow ();
Make.height.equalTo (@ (self.buttonSize.height)). Prioritylow ();
Make.width.lessThanOrEqualTo (self);
Make.height.lessThanOrEqualTo (self);
};
Finally remember the callback super method
[Super updateconstraints];
}
If you want to implement animation after you constrain a transform, you need to do the following
The notification requires an update constraint, but does not immediately execute
[self setneedsupdateconstraints];
Update constraint now to perform dynamic transform
//Update constraints We can animate the change
[self updateconstraintsifneeded];
//animation effect, set animation time
[UIView animatewithduration:0.4 animations:^{
[self layoutifneeded];
Text/Code Fun (Jane book author)
Original link: http://www.jianshu.com/p/1d1a1165bb04
Copyright belongs to the author, reproduced please contact the author to obtain authorization, and labeled "Jane book author."