"Last episode plot summary: Last episode we mainly try to write a simple layout of the design of the demo with a connected structure, first through the block way to achieve the chain call, and then encapsulate the constraints to add the layout of the block inside, to achieve a simple layout of the upper and lower left and right"
All right, ladies and gentlemen, it's time to add some layout features.
First, we consider a problem, because last episode we are mainly the default relative view is Superview, and are all with the default offset constant, and there is no multiple relationship, then how do we add the Toitem and multiplier these two parameters???
In what way to show the user better???
Thinking of ing .....
Well, the most straightforward way is to add two parameters, so it's all together .....
OK, I've encapsulated the code, and it looks like this ...
#import "Uiview+gcfadditions.h"@implementationUIView (gcfadditions)-(UIView * (^) (UIView *toitem,floatMultiplier,nsinteger space)) left{return[self addgcfconstraint:nslayoutattributeleft];}-(UIView * (^) (UIView *toitem,floatMultiplier,nsinteger space)) right{return[self addgcfconstraint:nslayoutattributeright];}-(UIView * (^) (UIView *toitem,floatMultiplier,nsinteger space)) top{return[self addgcfconstraint:nslayoutattributetop];}-(UIView * (^) (UIView *toitem,floatMultiplier,nsinteger space)) bottom{return[self addgcfconstraint:nslayoutattributebottom];}-(UIView * (^) (Nsinteger value)) width{return^(Nsinteger value) {if(value) {NSLog (@"%ld",(Long) value); } Else{NSLog (@"%ld",(Long) value); } self.translatesautoresizingmaskintoconstraints=NO; Nslayoutconstraint*constraint = [Nslayoutconstraint constraintwithitem:self attribute:nslayoutattributewidth relatedBy: Nslayoutrelationequal Toitem:nil Attribute:nslayoutattributenotanattribute Multiplier:1.0fConstant:value]; [Self addconstraint:constraint]; returnSelf ; };}-(UIView * (^) (Nsinteger value)) height{return^(Nsinteger value) {if(value) {NSLog (@"%ld",(Long) value); } Else{NSLog (@"%ld",(Long) value); } self.translatesautoresizingmaskintoconstraints=NO; Nslayoutconstraint*constraint = [Nslayoutconstraint constraintwithitem:self attribute:nslayoutattributeheight relatedBy: Nslayoutrelationequal Toitem:nil Attribute:nslayoutattributenotanattribute Multiplier:1.0fConstant:value]; [Self addconstraint:constraint]; returnSelf ; };}-(UIView * (^) (UIView *toitem,floatMultiplier,nsinteger space)) Addgcfconstraint: (nslayoutattribute) att{return^ (UIView *toitem,floatMultiplier,nsinteger space) { if(space) {NSLog (@"%ld",(Long) space); } Else{NSLog (@"%ld",(Long) space); } self.translatesautoresizingmaskintoconstraints=NO; Nslayoutconstraint*constainttop =[Nslayoutconstraint constraintwithitem:self Attribute:att relatedby:nslayoutrelationequal Toitem:toitem Attribute:att Multiplier:1.0Constant:space]; [Self.superview Addconstraint:constainttop]; returnSelf ; };}@end
Added width and height two properties
Inside the call, use becomes:
Self.view1.left (Self.view,1, -). Right (Self.view,1,- -). Top (Self.view,1, -). Height ( the); Self.view2.left (Self.view,1, -). Right (Self.view,1,- -). Top (Self.view1,1, -). Height ( the); Self.view3.left (Self.view,1, -). Right (Self.view,1,- -). Top (Self.view2,1, -). Height ( the);
Here we have completed the encapsulation of the native Nslayoutconstraint.
Automatic layout framework Design for iOS development (III.)