diff --git a/Sources/ConstraintMakerRelatable.swift b/Sources/ConstraintMakerRelatable.swift index 0a3d5ee2..d9323e16 100644 --- a/Sources/ConstraintMakerRelatable.swift +++ b/Sources/ConstraintMakerRelatable.swift @@ -86,7 +86,16 @@ public class ConstraintMakerRelatable { } return self.relatedTo(other, relation: .equal, file: file, line: line) } - + + @available(iOS 11.0, tvOS 11.0, macOS 11.0, *) + @discardableResult + public func equalToSuperviewSafeAreaLayoutGuide(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable { + guard let other = self.description.item.superview?.safeAreaLayoutGuide else { + fatalError("Expected superview but found nil when attempting make constraint `equalToSuperviewSafeAreaLayoutGuide`.") + } + return self.relatedTo(other, relation: .equal, file: file, line: line) + } + @discardableResult public func lessThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable { return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line) @@ -99,7 +108,16 @@ public class ConstraintMakerRelatable { } return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line) } - + + @available(iOS 11.0, tvOS 11.0, macOS 11.0, *) + @discardableResult + public func lessThanOrEqualToSuperviewSafeAreaLayoutGuide(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable { + guard let other = self.description.item.superview?.safeAreaLayoutGuide else { + fatalError("Expected superview but found nil when attempting make constraint `lessThanOrEqualToSuperviewSafeAreaLayoutGuide`.") + } + return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line) + } + @discardableResult public func greaterThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable { return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line) @@ -112,4 +130,13 @@ public class ConstraintMakerRelatable { } return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line) } + + @available(iOS 11.0, tvOS 11.0, macOS 11.0, *) + @discardableResult + public func greaterThanOrEqualToSuperviewSafeAreaLayoutGuide(_ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable { + guard let other = self.description.item.superview?.safeAreaLayoutGuide else { + fatalError("Expected superview but found nil when attempting make constraint `greaterThanOrEqualToSuperviewSafeAreaLayoutGuide`.") + } + return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line) + } } diff --git a/Tests/SnapKitTests/Tests.swift b/Tests/SnapKitTests/Tests.swift index 99ec6338..325f3bff 100644 --- a/Tests/SnapKitTests/Tests.swift +++ b/Tests/SnapKitTests/Tests.swift @@ -768,4 +768,38 @@ class SnapKitTests: XCTestCase { let higherPriority: ConstraintPriority = ConstraintPriority.high.advanced(by: 1) XCTAssertEqual(higherPriority.value, highPriority.value + 1) } + + @available(iOS 11.0, tvOS 11.0, macOS 11.0, *) + func testSafeAreaLayoutGuide() { + let v1 = View() + self.container.addSubview(v1) + + v1.snp.makeConstraints { make in + make.verticalEdges.equalToSuperviewSafeAreaLayoutGuide() + make.leading.lessThanOrEqualToSuperviewSafeAreaLayoutGuide() + make.trailing.greaterThanOrEqualToSuperviewSafeAreaLayoutGuide() + } + + XCTAssertEqual(container.snp_constraints.count, 4, "Should have 4 constraints installed") + XCTAssertNotNil(container.constraints.first { + $0.firstAttribute == .leading && + $0.secondAttribute == .leading && + $0.relation == .lessThanOrEqual + }) + XCTAssertNotNil(container.constraints.first { + $0.firstAttribute == .trailing && + $0.secondAttribute == .trailing && + $0.relation == .greaterThanOrEqual + }) + XCTAssertNotNil(container.constraints.first { + $0.firstAttribute == .top && + $0.secondAttribute == .top && + $0.relation == .equal + }) + XCTAssertNotNil(container.constraints.first { + $0.firstAttribute == .bottom && + $0.secondAttribute == .bottom && + $0.relation == .equal + }) + } }