From 63bf462e8107872e3c51e9c5400670e591072018 Mon Sep 17 00:00:00 2001 From: Felix Lapalme Date: Sun, 25 Dec 2016 06:37:38 -0500 Subject: [PATCH] Make ConstraintMakerRelatable targets work with UILayoutGuide (#372) * Make ConstraintMakerRelatable targets work with UILayoutGuide * Fix unit tests on macOS * Fix the OS API version availability directives --- Source/ConstraintMakerRelatable.swift | 3 +++ Source/ConstraintRelatableTarget.swift | 4 ++++ Tests/Tests.swift | 18 ++++++++---------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Source/ConstraintMakerRelatable.swift b/Source/ConstraintMakerRelatable.swift index 6b94e1d..98c7158 100644 --- a/Source/ConstraintMakerRelatable.swift +++ b/Source/ConstraintMakerRelatable.swift @@ -57,6 +57,9 @@ public class ConstraintMakerRelatable { } else if let other = other as? ConstraintConstantTarget { related = ConstraintItem(target: nil, attributes: ConstraintAttributes.none) constant = other + } else if #available(iOS 9.0, OSX 10.11, *), let other = other as? ConstraintLayoutGuide { + related = ConstraintItem(target: other, attributes: ConstraintAttributes.none) + constant = 0.0 } else { fatalError("Invalid constraint. (\(file), \(line))") } diff --git a/Source/ConstraintRelatableTarget.swift b/Source/ConstraintRelatableTarget.swift index 4d1e37d..6976367 100644 --- a/Source/ConstraintRelatableTarget.swift +++ b/Source/ConstraintRelatableTarget.swift @@ -60,3 +60,7 @@ extension ConstraintItem: ConstraintRelatableTarget { extension ConstraintView: ConstraintRelatableTarget { } + +@available(iOS 9.0, OSX 10.11, *) +extension ConstraintLayoutGuide: ConstraintRelatableTarget { +} diff --git a/Tests/Tests.swift b/Tests/Tests.swift index 651ac69..564be53 100644 --- a/Tests/Tests.swift +++ b/Tests/Tests.swift @@ -59,21 +59,21 @@ class SnapKitTests: XCTestCase { XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed") } - - #if os(iOS) || os(tvOS) + func testGuideMakeConstraints() { - guard #available(iOS 9.0, *) else { return } + guard #available(iOS 9.0, OSX 10.11, *) else { return } let v1 = View() - let g1 = UILayoutGuide() + + let g1 = ConstraintLayoutGuide() self.container.addSubview(v1) self.container.addLayoutGuide(g1) - + v1.snp.makeConstraints { (make) -> Void in - make.top.equalTo(g1.snp.top).offset(50) + make.top.equalTo(g1).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 @@ -83,8 +83,7 @@ class SnapKitTests: XCTestCase { XCTAssertEqual(self.container.snp_constraints.count, 6, "Should have 6 constraints installed") } - #endif - + func testMakeImpliedSuperviewConstraints() { let v1 = View() let v2 = View() @@ -130,7 +129,6 @@ class SnapKitTests: XCTestCase { } - func testRemakeConstraints() { let v1 = View() let v2 = View()