Automatic layout framework Design for iOS development (III.)

Source: Internet
Author: User

"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.)

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.