From fa01db45872f8d8c0f67b07c3f46222427fb4d09 Mon Sep 17 00:00:00 2001 From: Jonas Budelmann Date: Sun, 3 Nov 2013 18:09:35 +1100 Subject: [PATCH] adding new mas_updateConstraints method --- Masonry/MASCompositeConstraint.m | 2 ++ Masonry/MASConstraint.h | 5 +++++ Masonry/MASConstraintMaker.h | 5 +++++ Masonry/MASConstraintMaker.m | 1 + Masonry/MASViewConstraint.m | 7 ++++++- Masonry/View+MASAdditions.h | 11 +++++++++++ Masonry/View+MASAdditions.m | 8 ++++++++ Masonry/View+MASShorthandAdditions.h | 5 +++++ 8 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Masonry/MASCompositeConstraint.m b/Masonry/MASCompositeConstraint.m index a41b7b7d..e8725a75 100644 --- a/Masonry/MASCompositeConstraint.m +++ b/Masonry/MASCompositeConstraint.m @@ -18,6 +18,7 @@ @interface MASCompositeConstraint () @implementation MASCompositeConstraint @synthesize delegate = _delegate; +@synthesize updateExisting = _updateExisting; - (id)initWithChildren:(NSArray *)children { self = [super init]; @@ -181,6 +182,7 @@ - (void)constraint:(id)constraint shouldBeReplacedWithConstraint: - (void)install { for (id constraint in self.childConstraints) { + constraint.updateExisting = self.updateExisting; [constraint install]; } } diff --git a/Masonry/MASConstraint.h b/Masonry/MASConstraint.h index dcc2ea0f..c08625b4 100644 --- a/Masonry/MASConstraint.h +++ b/Masonry/MASConstraint.h @@ -113,6 +113,11 @@ */ @property (nonatomic, copy, readonly) id (^key)(id key); +/** + * Whether or not to check for an existing constraint instead of adding constraint + */ +@property (nonatomic, assign) BOOL updateExisting; + /** * Creates a NSLayoutConstraint. The constraint is installed to the first view or the or the closest common superview of the first and second view. */ diff --git a/Masonry/MASConstraintMaker.h b/Masonry/MASConstraintMaker.h index 1a106449..75a95ae1 100644 --- a/Masonry/MASConstraintMaker.h +++ b/Masonry/MASConstraintMaker.h @@ -53,6 +53,11 @@ */ @property (nonatomic, strong, readonly) id center; +/** + * Whether or not to check for an existing constraint instead of adding constraint + */ +@property (nonatomic, assign) BOOL updateExisting; + /** * initialises the maker with a default view * diff --git a/Masonry/MASConstraintMaker.m b/Masonry/MASConstraintMaker.m index c8d929b2..2ced0b28 100644 --- a/Masonry/MASConstraintMaker.m +++ b/Masonry/MASConstraintMaker.m @@ -34,6 +34,7 @@ - (id)initWithView:(MAS_VIEW *)view { - (NSArray *)install { NSArray *constraints = self.constraints.copy; for (id constraint in constraints) { + constraint.updateExisting = self.updateExisting; [constraint install]; } [self.constraints removeAllObjects]; diff --git a/Masonry/MASViewConstraint.m b/Masonry/MASViewConstraint.m index 0d77d5af..e2b8eb54 100644 --- a/Masonry/MASViewConstraint.m +++ b/Masonry/MASViewConstraint.m @@ -28,6 +28,7 @@ @interface MASViewConstraint () @implementation MASViewConstraint @synthesize delegate = _delegate; +@synthesize updateExisting = _updateExisting; - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute { self = [super init]; @@ -295,7 +296,11 @@ - (void)install { self.installedView = firstLayoutItem; } - MASLayoutConstraint *existingConstraint = [self layoutConstraintSimiliarTo:layoutConstraint]; + + MASLayoutConstraint *existingConstraint = nil; + if (self.updateExisting) { + existingConstraint = [self layoutConstraintSimiliarTo:layoutConstraint]; + } if (existingConstraint) { // just update the constant existingConstraint.constant = layoutConstraint.constant; diff --git a/Masonry/View+MASAdditions.h b/Masonry/View+MASAdditions.h index 64c9981a..90ceb5e3 100644 --- a/Masonry/View+MASAdditions.h +++ b/Masonry/View+MASAdditions.h @@ -55,4 +55,15 @@ */ - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; +/** + * Creates a MASConstraintMaker with the callee view. + * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. + * If an existing constraint exists then it will be updated instead. + * + * @param block scope within which you can build up the constraints which you wish to apply to the view. + * + * @return Array of created/updated MASConstraints + */ +- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; + @end diff --git a/Masonry/View+MASAdditions.m b/Masonry/View+MASAdditions.m index 7b285c11..201c3586 100644 --- a/Masonry/View+MASAdditions.m +++ b/Masonry/View+MASAdditions.m @@ -18,6 +18,14 @@ - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { return [constraintMaker install]; } +- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block { + self.translatesAutoresizingMaskIntoConstraints = NO; + MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; + block(constraintMaker); + constraintMaker.updateExisting = YES; + return [constraintMaker install]; +} + #pragma mark - NSLayoutAttribute properties - (MASViewAttribute *)mas_left { diff --git a/Masonry/View+MASShorthandAdditions.h b/Masonry/View+MASShorthandAdditions.h index b70f0f45..c527a2f1 100644 --- a/Masonry/View+MASShorthandAdditions.h +++ b/Masonry/View+MASShorthandAdditions.h @@ -29,6 +29,7 @@ @property (nonatomic, strong, readonly) MASViewAttribute *baseline; - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; +- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; @end @@ -55,6 +56,10 @@ MAS_ATTR_FORWARD(baseline); return [self mas_makeConstraints:block]; } +- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { + return [self mas_updateConstraints:block]; +} + @end #endif