Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A utility category for utilizing UIKit Auto Layout features efficiently.
//
// UIView+ConstraintsUtil.h
//
// Created by numata on 2014/01/02.
// Copyright (c) 2014 Sazameki and Satoshi Numata, Ph.D. All rights reserved.
//
@import UIKit;
@interface UIView (ConstraintsUtil)
- (NSLayoutConstraint *)addConstraintToAlignCenterHolizontally;
- (NSLayoutConstraint *)addConstraintToAlignCenterVertically;
- (NSLayoutConstraint *)addConstraintToFitToTopEdge;
- (NSLayoutConstraint *)addConstraintToFitToLeftEdge;
- (NSLayoutConstraint *)addConstraintToFitToRightEdge;
- (NSLayoutConstraint *)addConstraintToFitToBottomEdge;
- (NSLayoutConstraint *)addConstraintToFitToTopEdgeWithPadding:(CGFloat)padding;
- (NSLayoutConstraint *)addConstraintToFitToLeftEdgeWithPadding:(CGFloat)padding;
- (NSLayoutConstraint *)addConstraintToFitToRightEdgeWithPadding:(CGFloat)padding;
- (NSLayoutConstraint *)addConstraintToFitToBottomEdgeWithPadding:(CGFloat)padding;
- (NSLayoutConstraint *)addConstraintToFixWidth;
- (NSLayoutConstraint *)addConstraintToFixHeight;
@end
//
// UIView+ConstraintsUtil.m
//
// Created by numata on 2014/01/02.
// Copyright (c) 2014 Sazameki and Satoshi Numata, Ph.D. All rights reserved.
//
#import "UIView+ConstraintsUtil.h"
@implementation UIView (ConstraintsUtil)
- (NSLayoutConstraint *)addConstraintToSuperviewWithAttribute:(NSLayoutAttribute)attribute constant:(CGFloat)constant
{
self.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:attribute
relatedBy:NSLayoutRelationEqual
toItem:self.superview
attribute:attribute
multiplier:1.0
constant:constant];
[self.superview addConstraint:constraint];
return constraint;
}
- (NSLayoutConstraint *)addConstraintToSuperviewWithAttribute:(NSLayoutAttribute)attribute
{
return [self addConstraintToSuperviewWithAttribute:attribute constant:0.0];
}
- (NSLayoutConstraint *)addConstraintWithAttribute:(NSLayoutAttribute)attribute constant:(CGFloat)constant
{
self.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:attribute
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:constant];
[self addConstraint:constraint];
return constraint;
}
- (NSLayoutConstraint *)addConstraintWithAttribute:(NSLayoutAttribute)attribute
{
return [self addConstraintWithAttribute:attribute constant:0.0];
}
- (NSLayoutConstraint *)addConstraintToAlignCenterHolizontally
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeCenterX];
}
- (NSLayoutConstraint *)addConstraintToAlignCenterVertically
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeCenterY];
}
- (NSLayoutConstraint *)addConstraintToFitToTopEdge
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeTop];
}
- (NSLayoutConstraint *)addConstraintToFitToLeftEdge
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeLeading];
}
- (NSLayoutConstraint *)addConstraintToFitToRightEdge
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeTrailing];
}
- (NSLayoutConstraint *)addConstraintToFitToBottomEdge
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeBottom];
}
- (NSLayoutConstraint *)addConstraintToFitToTopEdgeWithPadding:(CGFloat)padding
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeTop constant:padding];
}
- (NSLayoutConstraint *)addConstraintToFitToLeftEdgeWithPadding:(CGFloat)padding
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeLeading constant:padding];
}
- (NSLayoutConstraint *)addConstraintToFitToRightEdgeWithPadding:(CGFloat)padding
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeTrailing constant:-padding];
}
- (NSLayoutConstraint *)addConstraintToFitToBottomEdgeWithPadding:(CGFloat)padding
{
return [self addConstraintToSuperviewWithAttribute:NSLayoutAttributeBottom constant:-padding];
}
- (NSLayoutConstraint *)addConstraintToFixWidth
{
return [self addConstraintWithAttribute:NSLayoutAttributeWidth constant:self.frame.size.width];
}
- (NSLayoutConstraint *)addConstraintToFixHeight
{
return [self addConstraintWithAttribute:NSLayoutAttributeHeight constant:self.frame.size.height];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment