IOS-Frameworks-UIKit-NSLayoutConstraint 布局相关工具类 参考《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 *constraintin self.constraints)

    {

       if ([aConstraint isEqualToLayoutConstraint:constraint])

        {

           return constraint;

        }

    }

    

   for (UIView *viewin [self superviews])

    {

       for(NSLayoutConstraint *constraintin view.constraints)

        {

           if ([aConstraint isEqualToLayoutConstraint:constraint])

            {

               return constraint;

            }

        }

    }

    return nil;

}


-(BOOL)removeMatchingConstraint:(NSLayoutConstraint*)aConstraint

{

   for(NSLayoutConstraint *constraintin self.constraints)

    {

       if ([aConstraint isEqualToLayoutConstraint:constraint])

        {

            [selfremoveConstraint:constraint];

           return YES;

        }

    }

    

   for (UIView *viewin [self superviews])

    {

       for(NSLayoutConstraint *constraintin view.constraints)

        {

           if ([aConstraint isEqualToLayoutConstraint:constraint])

            {

                [viewremoveConstraint: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.firstViewaddConstraint:self];

       return YES;

    }

    

    UIView * view = [self.firstViewnearestCommonAncestorToView:self.secondView];

   if (view)

    {

        [viewaddConstraint:self];

       return YES;

    }

   else

    {

       return NO;

    }

}


-(BOOL)installWithPriority:(UILayoutPriority)Priority

{

   self.priority = Priority;

    return [selfinstall];

}


-(void)remove

{

    if ([self.firstView.constraintscontainsObject:self])

    {

        [self.firstViewremoveConstraint:self];

       return;

    }

    

    if ([self.secondView.constraintscontainsObject:self])

    {

        [self.secondViewremoveConstraint:self];

       return;

    }

    

    UIView * tempView = [self.firstViewnearestCommonAncestorToView:self.secondView];

   if (tempView)

    {

        if ([tempView.constraintscontainsObject:self])

        {

            [tempViewremoveConstraint:self];

        }

    }

    

   for (UIView *viewin [self.firstViewsuperviews])

    {

        if ([view.constraintscontainsObject:self])

        {

            [viewremoveConstraint:self];

           return;

        }

    }

    

   for (UIView *viewin [self.secondViewsuperviews])

    {

        if ([view.constraintscontainsObject:self])

        {

            [viewremoveConstraint:self];

           return;

        }

    }

}


-(void)removeMatchingConstraint

{

    if (![self.firstViewremoveMatchingConstraint:self])

    {

        [self.secondViewremoveMatchingConstraint: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];

    [tempConinstall];

}


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

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

{

    [selflayoutTopOfView:viewOne equalBottomOfView:viewTwoaddConstant:0];

}


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

{

    NSLayoutConstraint *tempCon = [NSLayoutConstraintconstraintWithItem:viewOne attribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:viewTwo attribute:NSLayoutAttributeBottommultiplier:1.0constant:constant];

    [tempConinstall];

}

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

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

{

    [selflayoutCenterXOfView:viewOne equalCenterXOfView:viewTwoaddConstant: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 = [[NSMutableArrayalloc]init];

   UIView * view = self;

   while (view.superview)

    {

        [superViewsaddObject: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 = [selfsuperviews];

   NSArray *superviews2 = [aView superviews];

   for (UIView *tempViewin 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.firstItemisKindOfClass:[UIViewclass]])

    {

       return self.firstItem;

    }

    return nil;

}


//Cast the second item to a view

-(UIView*)secondView

{

    if ([self.secondItemisKindOfClass:[UIViewclass]])

    {

       return self.secondItem;

    }

    return nil;

}


-(BOOL)isUnary

{

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

    {

       return YES;

    }

    return NO;

}


@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值