Quellcode durchsuchen

Merge pull request #388 from iwill/master

Added inset property
Robert Payne vor 8 Jahren
Ursprung
Commit
7a59f9c3ef

+ 6 - 0
Masonry/MASCompositeConstraint.m

@@ -129,6 +129,12 @@
     }
     }
 }
 }
 
 
+- (void)setInset:(CGFloat)inset {
+    for (MASConstraint *constraint in self.childConstraints) {
+        constraint.inset = inset;
+    }
+}
+
 - (void)setOffset:(CGFloat)offset {
 - (void)setOffset:(CGFloat)offset {
     for (MASConstraint *constraint in self.childConstraints) {
     for (MASConstraint *constraint in self.childConstraints) {
         constraint.offset = offset;
         constraint.offset = offset;

+ 14 - 0
Masonry/MASConstraint.h

@@ -24,6 +24,13 @@
  */
  */
 - (MASConstraint * (^)(MASEdgeInsets insets))insets;
 - (MASConstraint * (^)(MASEdgeInsets insets))insets;
 
 
+/**
+ *	Modifies the NSLayoutConstraint constant,
+ *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
+ *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
+ */
+- (MASConstraint * (^)(CGFloat inset))inset;
+
 /**
 /**
  *	Modifies the NSLayoutConstraint constant,
  *	Modifies the NSLayoutConstraint constant,
  *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
@@ -163,6 +170,13 @@
  */
  */
 - (void)setInsets:(MASEdgeInsets)insets;
 - (void)setInsets:(MASEdgeInsets)insets;
 
 
+/**
+ *	Modifies the NSLayoutConstraint constant,
+ *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
+ *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
+ */
+- (void)setInset:(CGFloat)inset;
+
 /**
 /**
  *	Modifies the NSLayoutConstraint constant,
  *	Modifies the NSLayoutConstraint constant,
  *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
  *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following

+ 9 - 0
Masonry/MASConstraint.m

@@ -92,6 +92,13 @@
     };
     };
 }
 }
 
 
+- (MASConstraint * (^)(CGFloat))inset {
+    return ^id(CGFloat inset){
+        self.inset = inset;
+        return self;
+    };
+}
+
 - (MASConstraint * (^)(CGSize))sizeOffset {
 - (MASConstraint * (^)(CGSize))sizeOffset {
     return ^id(CGSize offset) {
     return ^id(CGSize offset) {
         self.sizeOffset = offset;
         self.sizeOffset = offset;
@@ -269,6 +276,8 @@
 
 
 - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); }
 - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); }
 
 
+- (void)setInset:(CGFloat __unused)inset { MASMethodNotImplemented(); }
+
 - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); }
 - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); }
 
 
 - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); }
 - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); }

+ 4 - 0
Masonry/MASViewConstraint.m

@@ -254,6 +254,10 @@ static char kInstalledConstraintsKey;
     }
     }
 }
 }
 
 
+- (void)setInset:(CGFloat)inset {
+    [self setInsets:(MASEdgeInsets){.top = inset, .left = inset, .bottom = inset, .right = inset}];
+}
+
 - (void)setOffset:(CGFloat)offset {
 - (void)setOffset:(CGFloat)offset {
     self.layoutConstant = offset;
     self.layoutConstant = offset;
 }
 }

+ 23 - 1
Tests/Specs/MASCompositeConstraintSpec.m

@@ -133,6 +133,28 @@ SpecBegin(MASCompositeConstraint) {
     expect([children[5] layoutConstant]).to.equal(0);
     expect([children[5] layoutConstant]).to.equal(0);
 };
 };
 
 
+- (void)testModifyInsetOnAppropriateChildren {
+    NSArray *children = @[
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_right],
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_top],
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_bottom],
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_left],
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_baseline],
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_width],
+                          ];
+    composite = [[MASCompositeConstraint alloc] initWithChildren:children];
+    composite.delegate = delegate;
+    
+    composite.with.inset(1);
+    
+    expect([children[0] layoutConstant]).to.equal(-1);
+    expect([children[1] layoutConstant]).to.equal(1);
+    expect([children[2] layoutConstant]).to.equal(-1);
+    expect([children[3] layoutConstant]).to.equal(1);
+    expect([children[4] layoutConstant]).to.equal(0);
+    expect([children[5] layoutConstant]).to.equal(0);
+};
+
 - (void)testUninstall {
 - (void)testUninstall {
     NSArray *children = @[
     NSArray *children = @[
         [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_leading],
         [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_leading],
@@ -193,4 +215,4 @@ SpecBegin(MASCompositeConstraint) {
     expect(newChild.delegate).to.beIdenticalTo(composite);
     expect(newChild.delegate).to.beIdenticalTo(composite);
 }
 }
 
 
-SpecEnd
+SpecEnd