Browse Source

Finish support for UILayoutGuide via Constraint MAker

Robert Payne 9 years ago
parent
commit
d32a47f0dd
3 changed files with 53 additions and 3 deletions
  1. 9 3
      Source/ConstraintDSL.swift
  2. 21 0
      Source/ConstraintLayoutGuideDSL.swift
  3. 23 0
      Tests/Tests.swift

+ 9 - 3
Source/ConstraintDSL.swift

@@ -49,12 +49,12 @@ extension ConstraintDSL {
 private var labelKey: UInt8 = 0
 
 
-public protocol ConstraintAttributesDSL: ConstraintDSL {
+public protocol ConstraintBasicAttributesDSL : ConstraintDSL {
 }
-extension ConstraintAttributesDSL {
+extension ConstraintBasicAttributesDSL {
     
     // MARK: Basics
-
+    
     public var left: ConstraintItem {
         return ConstraintItem(target: self.target, attributes: ConstraintAttributes.left)
     }
@@ -107,6 +107,12 @@ extension ConstraintAttributesDSL {
         return ConstraintItem(target: self.target, attributes: ConstraintAttributes.center)
     }
     
+}
+
+public protocol ConstraintAttributesDSL : ConstraintBasicAttributesDSL {
+}
+extension ConstraintAttributesDSL {
+    
     // MARK: Baselines
     
     @available(*, deprecated:3.0, message:"Use .lastBaseline instead")

+ 21 - 0
Source/ConstraintLayoutGuideDSL.swift

@@ -31,6 +31,27 @@
 @available(iOS 9.0, *)
 public struct ConstraintLayoutGuideDSL: ConstraintAttributesDSL {
     
+    @discardableResult
+    public func prepareConstraints(_ closure: (_ make: ConstraintMaker) -> Void) -> [Constraint] {
+        return ConstraintMaker.prepareConstraints(item: self.guide, closure: closure)
+    }
+    
+    public func makeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
+        ConstraintMaker.makeConstraints(item: self.guide, closure: closure)
+    }
+    
+    public func remakeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
+        ConstraintMaker.remakeConstraints(item: self.guide, closure: closure)
+    }
+    
+    public func updateConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
+        ConstraintMaker.updateConstraints(item: self.guide, closure: closure)
+    }
+    
+    public func removeConstraints() {
+        ConstraintMaker.removeConstraints(item: self.guide)
+    }
+    
     public var target: AnyObject? {
         return self.guide
     }

+ 23 - 0
Tests/Tests.swift

@@ -60,6 +60,29 @@ class SnapKitTests: XCTestCase {
         
     }
     
+    func testGuideMakeConstraints() {
+        let v1 = View()
+        let g1 = UILayoutGuide()
+        
+        self.container.addSubview(v1)
+        self.container.addLayoutGuide(g1)
+        
+        v1.snp.makeConstraints { (make) -> Void in
+            make.top.equalTo(g1.snp.top).offset(50)
+            make.left.equalTo(g1.snp.top).offset(50)
+            return
+        }
+        
+        XCTAssertEqual(self.container.snp_constraints.count, 2, "Should have 2 constraints installed")
+        
+        g1.snp.makeConstraints { (make) -> Void in
+            make.edges.equalTo(v1)
+            return
+        }
+        
+        XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed")
+    }
+    
     func testMakeImpliedSuperviewConstraints() {
         let v1 = View()
         let v2 = View()