Skip to content

Commit

Permalink
adding new mas_updateConstraints method
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudkite committed Nov 3, 2013
1 parent c940a56 commit fa01db4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Masonry/MASCompositeConstraint.m
Expand Up @@ -18,6 +18,7 @@ @interface MASCompositeConstraint () <MASConstraintDelegate>
@implementation MASCompositeConstraint

@synthesize delegate = _delegate;
@synthesize updateExisting = _updateExisting;

- (id)initWithChildren:(NSArray *)children {
self = [super init];
Expand Down Expand Up @@ -181,6 +182,7 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

- (void)install {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.updateExisting = self.updateExisting;
[constraint install];
}
}
Expand Down
5 changes: 5 additions & 0 deletions Masonry/MASConstraint.h
Expand Up @@ -113,6 +113,11 @@
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^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.
*/
Expand Down
5 changes: 5 additions & 0 deletions Masonry/MASConstraintMaker.h
Expand Up @@ -53,6 +53,11 @@
*/
@property (nonatomic, strong, readonly) id<MASConstraint> 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
*
Expand Down
1 change: 1 addition & 0 deletions Masonry/MASConstraintMaker.m
Expand Up @@ -34,6 +34,7 @@ - (id)initWithView:(MAS_VIEW *)view {
- (NSArray *)install {
NSArray *constraints = self.constraints.copy;
for (id<MASConstraint> constraint in constraints) {
constraint.updateExisting = self.updateExisting;
[constraint install];
}
[self.constraints removeAllObjects];
Expand Down
7 changes: 6 additions & 1 deletion Masonry/MASViewConstraint.m
Expand Up @@ -28,6 +28,7 @@ @interface MASViewConstraint ()
@implementation MASViewConstraint

@synthesize delegate = _delegate;
@synthesize updateExisting = _updateExisting;

- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute {
self = [super init];
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions Masonry/View+MASAdditions.h
Expand Up @@ -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
8 changes: 8 additions & 0 deletions Masonry/View+MASAdditions.m
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions Masonry/View+MASShorthandAdditions.h
Expand Up @@ -29,6 +29,7 @@
@property (nonatomic, strong, readonly) MASViewAttribute *baseline;

- (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block;

@end

Expand All @@ -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

0 comments on commit fa01db4

Please sign in to comment.