Skip to content

Commit

Permalink
setter methods for NSLayoutConstraint constant proxies like offset, c…
Browse files Browse the repository at this point in the history
…enterOffset, insets, sizeOffset.
  • Loading branch information
Jonas Budelmann committed Jan 17, 2014
1 parent 9806c36 commit 2e923b5
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Examples/Masonry iOS Examples/MASExampleAnimatedView.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (void)animateWithInvertedInsets:(BOOL)invertedInsets {
int padding = invertedInsets ? 100 : self.padding;
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
for (id<MASConstraint> constraint in self.animatableConstraints) {
constraint.insets(paddingInsets);
constraint.insets = paddingInsets;
}

[UIView animateWithDuration:1 animations:^{
Expand Down
42 changes: 30 additions & 12 deletions Masonry/MASCompositeConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,28 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

- (id<MASConstraint> (^)(MASEdgeInsets))insets {
return ^id(MASEdgeInsets insets) {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.insets(insets);
}
self.insets = insets;
return self;
};
}

- (id<MASConstraint> (^)(CGFloat))offset {
return ^id(CGFloat offset) {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.offset(offset);
}
self.offset = offset;
return self;
};
}

- (id<MASConstraint> (^)(CGSize))sizeOffset {
return ^id(CGSize offset) {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.sizeOffset(offset);
}
self.sizeOffset = offset;
return self;
};
}

- (id<MASConstraint> (^)(CGPoint))centerOffset {
return ^id(CGPoint offset) {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.centerOffset(offset);
}
self.centerOffset = offset;
return self;
};
}
Expand Down Expand Up @@ -191,6 +183,32 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:
};
}

#pragma mark - NSLayoutConstraint constant setters

- (void)setInsets:(UIEdgeInsets)insets {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.insets = insets;
}
}

- (void)setOffset:(CGFloat)offset {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.offset = offset;
}
}

- (void)setSizeOffset:(CGSize)sizeOffset {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.sizeOffset = sizeOffset;
}
}

- (void)setCenterOffset:(CGPoint)centerOffset {
for (id<MASConstraint> constraint in self.childConstraints) {
constraint.centerOffset = centerOffset;
}
}

#pragma mark - MASConstraint

- (void)install {
Expand Down
76 changes: 55 additions & 21 deletions Masonry/MASConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,101 +17,130 @@
*/
@protocol MASConstraint <NSObject>

/**
* Usually MASConstraintMaker but could be a parent MASConstraint
*/
@property (nonatomic, weak) id<MASConstraintDelegate> delegate;
// Chaining Support

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^insets)(MASEdgeInsets insets);
- (id<MASConstraint> (^)(MASEdgeInsets insets))insets;

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeWidth, NSLayoutAttributeHeight
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^sizeOffset)(CGSize offset);
- (id<MASConstraint> (^)(CGSize offset))sizeOffset;

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^centerOffset)(CGPoint offset);

- (id<MASConstraint> (^)(CGPoint offset))centerOffset;

/**
* Modifies the NSLayoutConstraint constant
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^offset)(CGFloat offset);
- (id<MASConstraint> (^)(CGFloat offset))offset;

/**
* Sets the NSLayoutConstraint multiplier property
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^multipliedBy)(CGFloat multiplier);
- (id<MASConstraint> (^)(CGFloat multiplier))multipliedBy;

/**
* Sets the NSLayoutConstraint multiplier to 1.0/dividedBy
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^dividedBy)(CGFloat divider);
- (id<MASConstraint> (^)(CGFloat divider))dividedBy;

/**
* Sets the NSLayoutConstraint priority to a float or MASLayoutPriority
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^priority)(MASLayoutPriority priority);
- (id<MASConstraint> (^)(MASLayoutPriority priority))priority;

/**
* Sets the NSLayoutConstraint priority to MASLayoutPriorityLow
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityLow)();
- (id<MASConstraint> (^)())priorityLow;

/**
* Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityMedium)();
- (id<MASConstraint> (^)())priorityMedium;

/**
* Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityHigh)();
- (id<MASConstraint> (^)())priorityHigh;

/**
* Sets the constraint relation to NSLayoutRelationEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* see readme for more details.
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^equalTo)(id attr);
- (id<MASConstraint> (^)(id attr))equalTo;

/**
* Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* see readme for more details.
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^greaterThanOrEqualTo)(id attr);
- (id<MASConstraint> (^)(id attr))greaterThanOrEqualTo;

/**
* Sets the constraint relation to NSLayoutRelationLessThanOrEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* see readme for more details.
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^lessThanOrEqualTo)(id attr);
- (id<MASConstraint> (^)(id attr))lessThanOrEqualTo;

/**
* optional semantic property which has no effect but improves the readability of constraint
*/
@property (nonatomic, copy, readonly) id<MASConstraint> with;
- (id<MASConstraint>)with;

/**
* Sets the constraint debug name
*/
@property (nonatomic, copy, readonly) id<MASConstraint> (^key)(id key);
- (id<MASConstraint> (^)(id key))key;


// NSLayoutConstraint constant Setters
// for use outside of mas_updateConstraints/mas_makeConstraints blocks

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
*/
- (void)setInsets:(MASEdgeInsets)insets;

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeWidth, NSLayoutAttributeHeight
*/
- (void)setSizeOffset:(CGSize)sizeOffset;

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
*/
- (void)setCenterOffset:(CGPoint)centerOffset;

/**
* Modifies the NSLayoutConstraint constant
*/
- (void)setOffset:(CGFloat)offset;


// NSLayoutConstraint Installation support

#if TARGET_OS_MAC && !TARGET_OS_IPHONE
/**
Expand All @@ -126,7 +155,12 @@
@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.
* Usually MASConstraintMaker but could be a parent MASConstraint
*/
@property (nonatomic, weak) id<MASConstraintDelegate> delegate;

/**
* Creates a NSLayoutConstraint and adds it to the appropriate view.
*/
- (void)install;

Expand Down
98 changes: 58 additions & 40 deletions Masonry/MASViewConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,64 +95,28 @@ - (void)setSecondViewAttribute:(id)secondViewAttribute {

- (id<MASConstraint> (^)(MASEdgeInsets))insets {
return ^id(MASEdgeInsets insets){
NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
switch (layoutAttribute) {
case NSLayoutAttributeLeft:
self.layoutConstant = insets.left;
break;
case NSLayoutAttributeTop:
self.layoutConstant = insets.top;
break;
case NSLayoutAttributeBottom:
self.layoutConstant = -insets.bottom;
break;
case NSLayoutAttributeRight:
self.layoutConstant = -insets.right;
break;
default:
break;
}
self.insets = insets;
return self;
};
}

- (id<MASConstraint> (^)(CGSize))sizeOffset {
return ^id(CGSize offset) {
NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
switch (layoutAttribute) {
case NSLayoutAttributeWidth:
self.layoutConstant = offset.width;
break;
case NSLayoutAttributeHeight:
self.layoutConstant = offset.height;
break;
default:
break;
}
self.sizeOffset = offset;
return self;
};
}

- (id<MASConstraint> (^)(CGPoint))centerOffset {
return ^id(CGPoint offset) {
NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
switch (layoutAttribute) {
case NSLayoutAttributeCenterX:
self.layoutConstant = offset.x;
break;
case NSLayoutAttributeCenterY:
self.layoutConstant = offset.y;
break;
default:
break;
}
self.centerOffset = offset;
return self;
};
}

- (id<MASConstraint> (^)(CGFloat))offset {
return ^id(CGFloat offset){
self.layoutConstant = offset;
self.offset = offset;
return self;
};
}
Expand Down Expand Up @@ -277,6 +241,60 @@ - (void)setSecondViewAttribute:(id)secondViewAttribute {
};
}

#pragma mark - NSLayoutConstraint constant setters

- (void)setInsets:(UIEdgeInsets)insets {
NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
switch (layoutAttribute) {
case NSLayoutAttributeLeft:
self.layoutConstant = insets.left;
break;
case NSLayoutAttributeTop:
self.layoutConstant = insets.top;
break;
case NSLayoutAttributeBottom:
self.layoutConstant = -insets.bottom;
break;
case NSLayoutAttributeRight:
self.layoutConstant = -insets.right;
break;
default:
break;
}
}

- (void)setOffset:(CGFloat)offset {
self.layoutConstant = offset;
}

- (void)setSizeOffset:(CGSize)sizeOffset {
NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
switch (layoutAttribute) {
case NSLayoutAttributeWidth:
self.layoutConstant = sizeOffset.width;
break;
case NSLayoutAttributeHeight:
self.layoutConstant = sizeOffset.height;
break;
default:
break;
}
}

- (void)setCenterOffset:(CGPoint)centerOffset {
NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
switch (layoutAttribute) {
case NSLayoutAttributeCenterX:
self.layoutConstant = centerOffset.x;
break;
case NSLayoutAttributeCenterY:
self.layoutConstant = centerOffset.y;
break;
default:
break;
}
}

#pragma mark - MASConstraint

- (void)install {
Expand Down
2 changes: 0 additions & 2 deletions Tests/Masonry Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
DD717A5618442EC600FAA7A8 /* NSLayoutConstraint+MASDebugAdditionsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = DD717A4E18442EC600FAA7A8 /* NSLayoutConstraint+MASDebugAdditionsSpec.m */; };
DD717A5718442EC600FAA7A8 /* View+MASAdditionsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = DD717A5018442EC600FAA7A8 /* View+MASAdditionsSpec.m */; };
DD717A631844303200FAA7A8 /* GcovTestObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DD717A5F1844303200FAA7A8 /* GcovTestObserver.m */; };
DD717A641844303200FAA7A8 /* MasonryTests-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = DD717A601844303200FAA7A8 /* MasonryTests-Info.plist */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -265,7 +264,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DD717A641844303200FAA7A8 /* MasonryTests-Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 2e923b5

Please sign in to comment.