布局相关工具类 参考《AutoLayout开发秘籍》第二版

//

//  UIView+ConstranitMatching.h

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface UIView (ConstranitMatching)


/*

 查找与某一指定约束属性相同的约束。

 */

-(NSLayoutConstraint*)constraintMatchingConstraint:(NSLayoutConstraint*)aConstraint;


/*

 删除与某一指定约束属性相同的约束。

 */

-(BOOL)removeMatchingConstraint:(NSLayoutConstraint*)aConstraint;


@end


//

//  UIView+ConstranitMatching.m

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import "UIView+ConstranitMatching.h"

#import "NSLayoutConstraint+ConstraintMatching.h"

#import "UIView+HierarchySupport.h"


@implementation UIView (ConstranitMatching)


-(NSLayoutConstraint*)constraintMatchingConstraint:(NSLayoutConstraint*)aConstraint

{

    for(NSLayoutConstraint *constraint in self.constraints)

    {

        if ([aConstraint isEqualToLayoutConstraint:constraint])

        {

            return constraint;

        }

    }

    

    for (UIView *view in [self superviews])

    {

        for(NSLayoutConstraint *constraint in view.constraints)

        {

            if ([aConstraint isEqualToLayoutConstraint:constraint])

            {

                return constraint;

            }

        }

    }

    return nil;

}


-(BOOL)removeMatchingConstraint:(NSLayoutConstraint*)aConstraint

{

    for(NSLayoutConstraint *constraint in self.constraints)

    {

        if ([aConstraint isEqualToLayoutConstraint:constraint])

        {

            [self removeConstraint:constraint];

            return YES;

        }

    }

    

    for (UIView *view in [self superviews])

    {

        for(NSLayoutConstraint *constraint in view.constraints)

        {

            if ([aConstraint isEqualToLayoutConstraint:constraint])

            {

                [view removeConstraint:constraint];

                return YES;

            }

        }

    }

    return NO;

}


@end





//

//  NSLayoutConstraint+SelfInstall.h

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface NSLayoutConstraint (SelfInstall)


//自动安装。

-(BOOL)install;

-(BOOL)installWithPriority:(UILayoutPriority)Priority;


//移除自己。

-(void)remove;


/*

 1:移除与自己相匹配的约束。

 2:无需使用变量之乡某一约束,需要删除的时候,直接创建一个相同属性的约束removeMatchingConstraint即可。

 */

-(void)removeMatchingConstraint;


@end




//

//  NSLayoutConstraint+SelfInstall.m

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import "NSLayoutConstraint+SelfInstall.h"

#import "NSLayoutConstraint+ViewHierarchy.h"

#import "UIView+HierarchySupport.h"

#import "UIView+ConstranitMatching.h"


@implementation NSLayoutConstraint (SelfInstall)


/*

 unary/'junəri/adj. []一元的

 */

-(BOOL)install

{

    //Handle Unary constraint

    if (self.isUnary)

    {

        [self.firstView addConstraint:self];

        return YES;

    }

    

    UIView * view = [self.firstView nearestCommonAncestorToView:self.secondView];

    if (view)

    {

        [view addConstraint:self];

        return YES;

    }

    else

    {

        return NO;

    }

}


-(BOOL)installWithPriority:(UILayoutPriority)Priority

{

    self.priority = Priority;

    return [self install];

}


-(void)remove

{

    if ([self.firstView.constraints containsObject:self])

    {

        [self.firstView removeConstraint:self];

        return;

    }

    

    if ([self.secondView.constraints containsObject:self])

    {

        [self.secondView removeConstraint:self];

        return;

    }

    

    UIView * tempView = [self.firstView nearestCommonAncestorToView:self.secondView];

    if (tempView)

    {

        if ([tempView.constraints containsObject:self])

        {

            [tempView removeConstraint:self];

        }

    }

    

    for (UIView *view in [self.firstView superviews])

    {

        if ([view.constraints containsObject:self])

        {

            [view removeConstraint:self];

            return;

        }

    }

    

    for (UIView *view in [self.secondView superviews])

    {

        if ([view.constraints containsObject:self])

        {

            [view removeConstraint:self];

            return;

        }

    }

}


-(void)removeMatchingConstraint

{

    if (![self.firstView removeMatchingConstraint:self])

    {

        [self.secondView removeMatchingConstraint:self];

    }

}


@end




//

//  UIView+LayoutTool.h

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface LayoutTool:NSObject


//topbottom相关设置

+ (void)layoutTopOfView:(UIView*)viewOne equalTopOfView:(UIView*)viewTwo;

+ (void)layoutTopOfView:(UIView*)viewOne equalTopOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


+ (void)layoutBottomOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo;

+ (void)layoutBottomOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


+ (void)layoutTopOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo;

+ (void)layoutTopOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


//leftright相关设置

+ (void)layoutLeftOfView:(UIView*)viewOne equalLeftOfView:(UIView*)viewTwo;

+ (void)layoutLeftOfView:(UIView*)viewOne equalLeftOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


+ (void)layoutRightOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo;

+ (void)layoutRightOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


+ (void)layoutLeftOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo;

+ (void)layoutLeftOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


//中心点相关设置

+ (void)layoutCenterXOfView:(UIView*)viewOne equalCenterXOfView:(UIView*)viewTwo;

+ (void)layoutCenterXOfView:(UIView*)viewOne equalCenterXOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


+ (void)layoutCenterYOfView:(UIView*)viewOne equalCenterYOfView:(UIView*)viewTwo;

+ (void)layoutCenterYOfView:(UIView*)viewOne equalCenterYOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


//宽高相关设置

+ (void)layoutWidthOfView:(UIView*)viewOne equalTo:(CGFloat)width;


+ (void)layoutHeightOfView:(UIView*)viewOne equalTo:(CGFloat)Height;


+ (void)layoutWidthOfView:(UIView*)viewOne equalWidthOfView:(UIView*)viewTwo;

+ (void)layoutWidthOfView:(UIView*)viewOne equalWidthOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;


+ (void)layoutHeightOfView:(UIView*)viewOne equalHeightOfView:(UIView*)viewTwo;

+ (void)layoutHeightOfView:(UIView*)viewOne equalHeightOfView:(UIView*)viewTwo addConstant:(CGFloat)constant;

@end




//

//  UIView+LayoutTool.m

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import "LayoutTool.h"

#import "NSLayoutConstraint+SelfInstall.h"


@implementation LayoutTool

//-------------------------------------------------------------------------------

+ (void)layoutLeftOfView:(UIView*)viewOne equalLeftOfView:(UIView*)viewTwo

{

    [self layoutLeftOfView:viewOne equalLeftOfView:viewTwo addConstant:0];

}


+ (void)layoutLeftOfView:(UIView*)viewOne equalLeftOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeLeft multiplier:1.0 constant:constant];

    [tempCon install];

}


//-------------------------------------------------------------------------------

+ (void)layoutRightOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo

{

    [self layoutRightOfView:viewOne equalRightOfView:viewTwo addConstant:0];

}


+ (void)layoutRightOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeRight multiplier:1.0 constant:constant];

    [tempCon install];

}


//-------------------------------------------------------------------------------

+ (void)layoutTopOfView:(UIView*)viewOne equalTopOfView:(UIView*)viewTwo

{

    [self layoutTopOfView:viewOne equalTopOfView:viewTwo addConstant:0];

}


+ (void)layoutTopOfView:(UIView*)viewOne equalTopOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeTop multiplier:1.0 constant:constant];

    [tempCon install];

}


//-------------------------------------------------------------------------------

+ (void)layoutBottomOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo

{

    [self layoutBottomOfView:viewOne equalBottomOfView:viewTwo addConstant:0];

}


+ (void)layoutBottomOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeBottom multiplier:1.0 constant:constant];

    [tempCon install];

}


//-------------------------------------------------------------------------------

+ (void)layoutLeftOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo

{

    [self layoutLeftOfView:viewOne equalRightOfView:viewTwo addConstant:0];

}


+ (void)layoutLeftOfView:(UIView*)viewOne equalRightOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeRight multiplier:1.0 constant:constant];

    [tempCon install];

}


//-------------------------------------------------------------------------------

+ (void)layoutTopOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo

{

    [self layoutTopOfView:viewOne equalBottomOfView:viewTwo addConstant:0];

}


+ (void)layoutTopOfView:(UIView*)viewOne equalBottomOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeBottom multiplier:1.0 constant:constant];

    [tempCon install];

}

//-------------------------------------------------------------------------------

+ (void)layoutCenterXOfView:(UIView*)viewOne equalCenterXOfView:(UIView*)viewTwo

{

    [self layoutCenterXOfView:viewOne equalCenterXOfView:viewTwo addConstant:0];

}


+ (void)layoutCenterXOfView:(UIView*)viewOne equalCenterXOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:constant];

    [tempCon install];

}



+ (void)layoutCenterYOfView:(UIView*)viewOne equalCenterYOfView:(UIView*)viewTwo

{

    [self layoutCenterYOfView:viewOne equalCenterYOfView:viewTwo addConstant:0];

}


+ (void)layoutCenterYOfView:(UIView*)viewOne equalCenterYOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:constant];

    [tempCon install];

}


+ (void)layoutWidthOfView:(UIView*)viewOne equalWidthOfView:(UIView*)viewTwo

{

    [self layoutWidthOfView:viewOne equalWidthOfView:viewTwo addConstant:0];

}


+ (void)layoutWidthOfView:(UIView*)viewOne equalWidthOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeWidth multiplier:1.0 constant:constant];

    [tempCon install];

}


+ (void)layoutHeightOfView:(UIView*)viewOne equalHeightOfView:(UIView*)viewTwo

{

    [self layoutHeightOfView:viewOne equalHeightOfView:viewTwo addConstant:0];

}


+ (void)layoutHeightOfView:(UIView*)viewOne equalHeightOfView:(UIView*)viewTwo addConstant:(CGFloat)constant

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:viewTwo attribute:NSLayoutAttributeHeight multiplier:1.0 constant:constant];

    [tempCon install];

}


+ (void)layoutWidthOfView:(UIView*)viewOne equalTo:(CGFloat)width

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width];

    [tempCon install];

}


+ (void)layoutHeightOfView:(UIView*)viewOne equalTo:(CGFloat)Height

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraint constraintWithItem:viewOne attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:Height];

    [tempCon install];

}


@end




//

//  UIView+HierarchySupport.h

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import <UIKit/UIKit.h>


/*

 hierarchy /'haɪərɑrki/n. 等级制度;统治集团, 领导层;

 */


@interface UIView (HierarchySupport)


/*

 Return an array of all superviews.

 */

- (NSArray*)superviews;


/*

 Test if the current view has a superview relationship to a view

 */

- (BOOL)isAncestorOfView:(UIView*)aView;


/*

 Return the nearest common ancestor between self and another view

 */

- (UIView*)nearestCommonAncestorToView:(UIView*)aView;


@end



//

//  UIView+HierarchySupport.m

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import "UIView+HierarchySupport.h"


@implementation UIView (HierarchySupport)


/*

 Return an array of all superviews.

 */

- (NSArray*)superviews

{

    NSMutableArray * superViews = [[NSMutableArray alloc]init];

    UIView * view = self;

    while (view.superview)

    {

        [superViews addObject:view.superview];

        view = view.superview;

    }

    return superViews;

}


/*

 Test if the current view has a superview relationship to a view

 */

- (BOOL)isAncestorOfView:(UIView*)aView

{

    return [[aView superviews]containsObject:self];

}


/*

 Return the nearest common ancestor between self and another view

 */

- (UIView*)nearestCommonAncestorToView:(UIView*)aView

{

    if (aView == nil)

    {

        return nil;

    }

    

    //Check for same view

    if([self isEqual:aView])

    {

        return self;

    }

    

    //Check for direct superview relationship

    if ([self isAncestorOfView:aView])

    {

        return self;

    }

    if ([aView isAncestorOfView:self])

    {

        return aView;

    }

    

    NSArray *superviews1 = [self superviews];

    NSArray *superviews2 = [aView superviews];

    for (UIView *tempView in superviews1)

    {

        if ([superviews2 containsObject:tempView])

        {

            return tempView;

        }

    }

    

    return nil;

}


@end



//

//  NSLayoutConstraint+ConstraintMatching.h

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface NSLayoutConstraint (ConstraintMatching)


//This ignores any priority,looking only at y(R)mx+b

-(BOOL) isEqualToLayoutConstraint:(NSLayoutConstraint*)constraint;


@end



//

//  NSLayoutConstraint+ConstraintMatching.m

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import "NSLayoutConstraint+ConstraintMatching.h"


@implementation NSLayoutConstraint (ConstraintMatching)


//This ignores any priority,looking only at y(R)mx+b

-(BOOL) isEqualToLayoutConstraint:(NSLayoutConstraint*)constraint

{

    if ([self isEqual:constraint]) {

        return YES;

    }

    

    if (self.firstItem!=constraint.firstItem) {

        return NO;

    }

    

    if (self.secondItem!=constraint.secondItem) {

        return NO;

    }

    

    if (self.firstAttribute!=constraint.firstAttribute) {

        return NO;

    }

    

    if (self.secondAttribute!=constraint.secondAttribute) {

        return NO;

    }

    

    if (self.relation!=constraint.relation) {

        return NO;

    }

    

    if (self.multiplier!=constraint.multiplier) {

        return NO;

    }

    

    if (self.constant!=constraint.constant) {

        return NO;

    }

    

    return YES;

}


@end



//

//  NSLayoutConstraint+ViewHierarchy.h

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface NSLayoutConstraint (ViewHierarchy)


@property (nonatomic,readonly) UIView *firstView;

@property (nonatomic,readonly) UIView *secondView;

@property (nonatomic,readonly) BOOL isUnary;


@end



//

//  NSLayoutConstraint+ViewHierarchy.m

//  IosAutoLayout

//

//  Created by ranzhou on 15/8/8.

//  Copyright (c) 2015 ranzhou. All rights reserved.

//


#import "NSLayoutConstraint+ViewHierarchy.h"


@implementation NSLayoutConstraint (ViewHierarchy)


/*

 cast/kæst/

 vt. & vi. , , ;浇铸

 vt. 投射, ;分配(演戏剧等的)角色

 n. , , ,

 铸造物; 塑件

 石膏

 演员表, 全体演员

 n. 原主人不再要的衣服/casting vote决定票

 */

//Cast the first item to a view

-(UIView*)firstView

{

    if ([self.firstItem isKindOfClass:[UIView class]])

    {

        return self.firstItem;

    }

    return nil;

}


//Cast the second item to a view

-(UIView*)secondView

{

    if ([self.secondItem isKindOfClass:[UIView class]])

    {

        return self.secondItem;

    }

    return nil;

}


-(BOOL)isUnary

{

    if (self.firstView!=nil&&self.secondView==nil)

    {

        return YES;

    }

    return NO;

}


@end





你可能感兴趣的:(ios,工具类,自动布局)