Skip to content

Instantly share code, notes, and snippets.

@SuperY
Forked from ericcj/UIView+TLLayout.h
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuperY/bbb6b0e8fc4cb801570a to your computer and use it in GitHub Desktop.
Save SuperY/bbb6b0e8fc4cb801570a to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface UIView (TLLayout)
@property (nonatomic, strong) NSArray *hiddenConstraints;
// set hidden and remove any constraints involving this view from its superview
- (void)hideAndRemoveConstraints;
- (void)showAndRestoreConstraints;
@end
#import <objc/runtime.h>
#import "UIView+TLLayout.h"
MAKE_CATEGORIES_LOADABLE(UIView_TLLayout)
static char kTLHiddenConstraintsKey;
@implementation UIView (TLLayout)
@dynamic hiddenConstraints;
- (NSArray *)hiddenConstraints {
return objc_getAssociatedObject(self, &kTLHiddenConstraintsKey);
}
- (void)setHiddenConstraints:(NSArray *)constraints {
objc_setAssociatedObject(self, &kTLHiddenConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)hideAndRemoveConstraints
{
self.hidden = YES;
if (!self.hiddenConstraints) {
self.hiddenConstraints = [self.superview.constraints objectsPassingTest:^BOOL(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
return constraint.firstItem == self || constraint.secondItem == self;
}];
[self.superview removeConstraints:self.hiddenConstraints];
}
}
- (void)showAndRestoreConstraints
{
if (self.hiddenConstraints) {
[self.superview addConstraints:self.hiddenConstraints];
self.hiddenConstraints = nil;
}
self.hidden = NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment