Browse Source

Add activate/deactivate methods

Pavel Mazurin 11 years ago
parent
commit
9fa5d75a64

+ 16 - 0
Masonry/MASCompositeConstraint.m

@@ -162,4 +162,20 @@
     }
 }
 
+#if __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

+ 13 - 0
Masonry/MASConstraint.h

@@ -6,6 +6,7 @@
 //  Copyright (c) 2013 cloudling. All rights reserved.
 //
 
+#import <Availability.h>
 #import "MASUtilities.h"
 
 /**
@@ -181,6 +182,18 @@
  */
 - (void)uninstall;
 
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
+/**
+ *  Activates an NSLayoutConstraint.
+ */
+- (void)activate;
+
+/**
+ *  Deactivates previously installed NSLayoutConstraint.
+ */
+- (void)deactivate;
+#endif
+
 @end
 
 

+ 6 - 0
Masonry/MASConstraint.m

@@ -238,4 +238,10 @@
 
 - (void)uninstall { MASMethodNotImplemented(); }
 
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
+- (void)activate { MASMethodNotImplemented(); }
+
+- (void)deactivate { MASMethodNotImplemented(); }
+#endif
+
 @end

+ 14 - 0
Masonry/MASViewConstraint.m

@@ -354,4 +354,18 @@ static char kInstalledConstraintsKey;
     [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
 }
 
+#if __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

+ 29 - 1
Tests/Specs/MASViewConstraintSpec.m

@@ -509,12 +509,40 @@ SpecBegin(MASViewConstraint) {
     expect(superview.constraints).to.haveCountOf(0);
 }
 
+#if __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);
     
     expect(^{
-        id result = constraint.bottom;
+        __unused id result = constraint.bottom;
     }).to.raise(@"NSInternalInconsistencyException");
 }