Authored by Pavel Mazurin

Moved activate/deactivate functionality to install/uninstall

... ... @@ -162,20 +162,4 @@
}
}
#if defined(__IPHONE_8_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
- (void)activate
{
for (MASConstraint *constraint in self.childConstraints) {
[constraint activate];
}
}
- (void)deactivate
{
for (MASConstraint *constraint in self.childConstraints) {
[constraint deactivate];
}
}
#endif
@end
... ...
... ... @@ -182,18 +182,6 @@
*/
- (void)uninstall;
#if defined(__IPHONE_8_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
/**
* Activates an NSLayoutConstraint.
*/
- (void)activate;
/**
* Deactivates previously installed NSLayoutConstraint.
*/
- (void)deactivate;
#endif
@end
... ...
... ... @@ -238,10 +238,4 @@
- (void)uninstall { MASMethodNotImplemented(); }
#if defined(__IPHONE_8_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
- (void)activate { MASMethodNotImplemented(); }
- (void)deactivate { MASMethodNotImplemented(); }
#endif
@end
... ...
... ... @@ -102,8 +102,20 @@ static char kInstalledConstraintsKey;
self.hasLayoutRelation = YES;
}
- (BOOL)supportsActiveProperty {
return [self.layoutConstraint respondsToSelector:@selector(isActive)];
}
- (BOOL)isActive {
BOOL active = YES;
#ifdef __IPHONE_8_0
active = [self supportsActiveProperty] && [self.layoutConstraint isActive];
#endif
return active;
}
- (BOOL)hasBeenInstalled {
return self.layoutConstraint != nil;
return (self.layoutConstraint != nil) && [self isActive];
}
- (void)setSecondViewAttribute:(id)secondViewAttribute {
... ... @@ -273,9 +285,20 @@ static char kInstalledConstraintsKey;
#pragma mark - MASConstraint
- (void)install {
NSAssert(!self.hasBeenInstalled, @"Cannot install constraint more than once");
if (self.hasBeenInstalled) {
return;
}
MAS_VIEW *firstLayoutItem = self.firstViewAttribute.view;
#ifdef __IPHONE_8_0
if ([self supportsActiveProperty] && self.layoutConstraint) {
self.layoutConstraint.active = YES;
[firstLayoutItem.mas_installedConstraints addObject:self];
return;
}
#endif
NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
MAS_VIEW *secondLayoutItem = self.secondViewAttribute.view;
NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;
... ... @@ -348,24 +371,18 @@ static char kInstalledConstraintsKey;
}
- (void)uninstall {
[self.installedView removeConstraint:self.layoutConstraint];
self.layoutConstraint = nil;
self.installedView = nil;
#ifdef __IPHONE_8_0
if ([self.layoutConstraint respondsToSelector:@selector(setActive:)]) {
self.layoutConstraint.active = NO;
} else
#endif
{
[self.installedView removeConstraint:self.layoutConstraint];
self.layoutConstraint = nil;
self.installedView = nil;
}
[self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
}
#if defined(__IPHONE_8_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
- (void)activate
{
NSAssert(self.hasBeenInstalled, @"Can't activate constraint that is not installed yet.");
self.layoutConstraint.active = YES;
}
- (void)deactivate
{
NSAssert(self.hasBeenInstalled, @"Can't deactivate constraint that is not installed yet.");
self.layoutConstraint.active = NO;
}
#endif
@end
... ...
... ... @@ -152,31 +152,6 @@ SpecBegin(MASCompositeConstraint) {
expect(superview.constraints).to.haveCountOf(0);
}
#if defined(__IPHONE_8_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
- (void)testActivateDeactivate
{
NSArray *children = @[
[[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_leading],
[[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_trailing]
];
composite = [[MASCompositeConstraint alloc] initWithChildren:children];
composite.delegate = delegate;
MAS_VIEW *newView = MAS_VIEW.new;
[superview addSubview:newView];
//first equality statement
composite.equalTo(newView);
[composite install];
expect(superview.constraints).to.haveCountOf(2);
[composite deactivate];
expect(superview.constraints).to.haveCountOf(0);
[composite activate];
expect(superview.constraints).to.haveCountOf(2);
}
#endif
- (void)testAttributeChainingShouldCallDelegate {
NSArray *children = @[
[[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_left],
... ...
... ... @@ -509,34 +509,6 @@ SpecBegin(MASViewConstraint) {
expect(superview.constraints).to.haveCountOf(0);
}
#if defined(__IPHONE_8_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
- (void)testDeactivateConstraint {
MASViewAttribute *secondViewAttribute = otherView.mas_height;
constraint.equalTo(secondViewAttribute);
[constraint install];
expect(superview.constraints).to.haveCountOf(1);
expect(superview.constraints[0]).to.equal(constraint.layoutConstraint);
expect([superview.constraints[0] isActive]).to.beTruthy();
[constraint deactivate];
expect(superview.constraints).to.haveCountOf(0);
}
- (void)testActivateConstraint {
MASViewAttribute *secondViewAttribute = otherView.mas_height;
constraint.equalTo(secondViewAttribute);
[constraint install];
expect(superview.constraints).to.haveCountOf(1);
[constraint deactivate];
expect(superview.constraints).to.haveCountOf(0);
[constraint activate];
expect(superview.constraints).to.haveCountOf(1);
expect(superview.constraints[0]).to.equal(constraint.layoutConstraint);
}
#endif
- (void)testAttributeChainingShouldNotHaveRelation {
MASViewAttribute *secondViewAttribute = otherView.mas_top;
constraint.lessThanOrEqualTo(secondViewAttribute);
... ...