Created
April 25, 2014 01:55
-
-
Save robertjpayne/11275532 to your computer and use it in GitHub Desktop.
Tagging system for Masonry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage is as such: