Skip to content

Instantly share code, notes, and snippets.

@marciok
Created December 5, 2013 10:58
Show Gist options
  • Save marciok/5444d4d806a30b06e620 to your computer and use it in GitHub Desktop.
Save marciok/5444d4d806a30b06e620 to your computer and use it in GitHub Desktop.
A category to help work with constraints. Basically what I did is created getters methods to help retrieve a specific constraint, for example: a tableview bottom constraint without having to connect IBOutlets. It's still pretty basic with only 4 methods: - bottomConstraint - topConstraint - widthConstraint - heightConstraint
//
// UIView+Constraints.h
// Calendar
//
// Created by Marcio Klepacz on 12/3/13.
// Copyright (c) 2013 Any.DO. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (Constraints)
@property (strong, nonatomic, readonly) NSLayoutConstraint *bottomConstraint;
@property (strong, nonatomic, readonly) NSLayoutConstraint *topConstraint;
@property (strong, nonatomic, readonly) NSLayoutConstraint *widthConstraint;
@property (strong, nonatomic, readonly) NSLayoutConstraint *heightConstraint;
@end
//
// UIView+Constraints.m
// Calendar
//
// Created by Marcio Klepacz on 12/3/13.
// Copyright (c) 2013 Any.DO. All rights reserved.
//
#import "UIView+Constraints.h"
@implementation UIView (Constraints)
- (NSLayoutConstraint *) bottomConstraint
{
if ([self superviewHasConstraints:self.superview.constraints]) {
for (NSLayoutConstraint *constraint in self.superview.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeBottom) {
return constraint;
}
};
}
return nil;
}
- (NSLayoutConstraint *) topConstraint
{
if ([self superviewHasConstraints:self.superview.constraints]) {
for (NSLayoutConstraint *constraint in self.superview.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeTop) {
return constraint;
}
};
}
return nil;
}
- (NSLayoutConstraint *) widthConstraint
{
if ([self superviewHasConstraints:self.constraints]) {
for (NSLayoutConstraint *constraint in self.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeWidth) {
return constraint;
}
};
}
return nil;
}
- (NSLayoutConstraint *) heightConstraint
{
if ([self superviewHasConstraints:self.constraints]) {
for (NSLayoutConstraint *constraint in self.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
return constraint;
}
};
}
return nil;
}
#pragma mark -
#pragma private methods
- (BOOL) superviewHasConstraints:(NSArray *)superViewConstraints
{
if (superViewConstraints == nil || [superViewConstraints count] == 0)
return NO;
else
return YES;
}
@end
#import "UIView+Constraints.h"
// Test support
#import "UIView+ConstraintsPrivateDeclarations.h"
#import <SenTestingKit/SenTestingKit.h>
#import <Kiwi.h>
#import "TestUtils.h"
SPEC_BEGIN(UIViewConstraintsSpecs)
describe(@"UIView+Constraints ", ^{
__block UIView *sut;
__block UIView *sutSuperView;
__block NSLayoutConstraint *constraint;
beforeAll(^{
sut = [[UIView alloc] init];
sutSuperView = [[UIView alloc] init];
[sutSuperView addSubview:sut];
});
afterAll(^{
sut = nil;
sutSuperView = nil;
});
describe(@"Get bottomConstraint",^{
context(@"When the superview has constraint", ^{
beforeEach(^{
constraint = [NSLayoutConstraint constraintWithItem:sut.superview
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:sut
attribute:NSLayoutAttributeTop
multiplier:1
constant:1];
[sut.superview addConstraint:constraint];
});
afterEach(^{
[sut.superview removeConstraint:constraint];
});
it(@"should return a bottomConstraint", ^{
[[sut.bottomConstraint should] equal:constraint];
});
});
context(@"When don't have constraints", ^{
it(@"should return nil", ^{
[sut.bottomConstraint shouldBeNil];
});
});
});
describe(@"Get topConstraint",^{
context(@"When the superview has constraint", ^{
beforeEach(^{
constraint = [NSLayoutConstraint constraintWithItem:sut.superview
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:sut
attribute:NSLayoutAttributeBottom
multiplier:1
constant:1];
[sut.superview addConstraint:constraint];
});
afterEach(^{
[sut.superview removeConstraint:constraint];
});
it(@"should return a bottomConstraint", ^{
[[sut.topConstraint should] equal:constraint];
});
});
context(@"When don't have constraints", ^{
it(@"should return nil", ^{
[sut.topConstraint shouldBeNil];
});
});
});
describe(@"Get widthConstraint",^{
context(@"When the superview has constraint", ^{
beforeEach(^{
constraint = [NSLayoutConstraint constraintWithItem:sut
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:1];
[sut addConstraint:constraint];
});
afterEach(^{
[sut removeConstraint:constraint];
});
it(@"should return a bottomConstraint", ^{
[[sut.widthConstraint should] equal:constraint];
});
});
context(@"When don't have constraints", ^{
it(@"should return nil", ^{
[sut.widthConstraint shouldBeNil];
});
});
});
describe(@"Get heightConstraint",^{
context(@"When the superview has constraint", ^{
beforeEach(^{
constraint = [NSLayoutConstraint constraintWithItem:sut
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:1];
[sut addConstraint:constraint];
});
afterEach(^{
[sut removeConstraint:constraint];
});
it(@"should return a bottomConstraint", ^{
[[sut.heightConstraint should] equal:constraint];
});
});
context(@"When don't have constraints", ^{
it(@"should return nil", ^{
[sut.heightConstraint shouldBeNil];
});
});
});
});
SPEC_END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment