Skip to content

Instantly share code, notes, and snippets.

@robertjpayne
Created April 25, 2014 01:55
Show Gist options
  • Save robertjpayne/11275532 to your computer and use it in GitHub Desktop.
Save robertjpayne/11275532 to your computer and use it in GitHub Desktop.
Tagging system for Masonry
#import <UIKit/UIKit.h>
#import <Masonry/Masonry.h>
@interface UIView (MasonryTagging)
- (void)prepareConstraintsWithTag:(NSString *)tag block:(void (^)(MASConstraintMaker *make))block;
- (void)removePreparedConstraintsWithTag:(NSString *)tag;
- (void)installConstraintsWithTag:(NSString *)tag;
- (void)installConstraintsWithTag:(NSString *)tag recursive:(BOOL)recursive;
- (void)uninstallConstraintsWithTag:(NSString *)tag;
- (void)uninstallConstraintsWithTag:(NSString *)tag recursive:(BOOL)recursive;
@end
#import "UIView+MasonryTagging.h"
#import <objc/message.h>
@implementation UIView (MasonryTagging)
- (NSMutableDictionary *)mas_constraintsByTag {
static const void *mas_constraintsByTagKey = &mas_constraintsByTagKey;
NSMutableDictionary *d = objc_getAssociatedObject(self, mas_constraintsByTagKey);;
if(!d) {
d = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, mas_constraintsByTagKey, d, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return d;
}
- (void)prepareConstraintsWithTag:(NSString *)tag block:(void (^)(MASConstraintMaker *make))block {
NSParameterAssert(tag);
NSParameterAssert(block);
NSArray *constraints = [self makeConstraints:block];
for(MASConstraint *constraint in constraints) {
[constraint uninstall];
}
NSMutableArray *taggedConstraints = [self mas_constraintsByTag][tag];
if(!taggedConstraints) {
taggedConstraints = [constraints mutableCopy];
[self mas_constraintsByTag][tag] = taggedConstraints;
} else {
[taggedConstraints addObjectsFromArray:constraints];
}
}
- (void)removePreparedConstraintsWithTag:(NSString *)tag {
[self uninstallConstraintsWithTag:tag];
[[self mas_constraintsByTag][tag] removeAllObjects];
}
- (void)installConstraintsWithTag:(NSString *)tag {
NSArray *taggedConstraints = [self mas_constraintsByTag][tag];
[taggedConstraints makeObjectsPerformSelector:@selector(install)];
}
- (void)installConstraintsWithTag:(NSString *)tag recursive:(BOOL)recursive {
[self installConstraintsWithTag:tag];
if(recursive) {
for(UIView *subivew in self.subviews) {
[subivew installConstraintsWithTag:tag recursive:recursive];
}
}
}
- (void)uninstallConstraintsWithTag:(NSString *)tag {
NSArray *taggedConstraints = [self mas_constraintsByTag][tag];
[taggedConstraints makeObjectsPerformSelector:@selector(uninstall)];
}
- (void)uninstallConstraintsWithTag:(NSString *)tag recursive:(BOOL)recursive {
[self uninstallConstraintsWithTag:tag];
if(recursive) {
for(UIView *subivew in self.subviews) {
[subivew uninstallConstraintsWithTag:tag recursive:recursive];
}
}
}
@end
@robertjpayne
Copy link
Author

Usage is as such:

[view prepareConstraintsWithTag:@"keyboard" block:^(MASConstraintMaker *make) {
   make.centerY.equalTo(otherView).with.offset(50.0);
}];

[view installConstraintsWithTag:@"keyboard" recursive:YES];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment