diff --git a/Sources/Layout/UIKit/UIView+AutoLayout.swift b/Sources/Layout/UIKit/UIView+AutoLayout.swift index b102efb0..7de90412 100644 --- a/Sources/Layout/UIKit/UIView+AutoLayout.swift +++ b/Sources/Layout/UIKit/UIView+AutoLayout.swift @@ -324,11 +324,14 @@ extension UIView { public func edgeConstraints( insets: DirectionalInsets ) -> [NSLayoutConstraint] { - [ - constraint(toSuperview: .leading, constant: insets.leading), - constraint(toSuperview: .trailing, constant: -insets.trailing), - constraint(toSuperview: .top, constant: insets.top), - constraint(toSuperview: .bottom, constant: -insets.bottom) + assert(superview != nil, "edgeConstraints(insets:) requires superview") + guard let superview: UIView + else { return [] } + return [ + leading.constraint(equalTo: superview.leading, constant: insets.leading), + trailing.constraint(equalTo: superview.trailing, constant: -insets.trailing), + top.constraint(equalTo: superview.top, constant: insets.top), + bottom.constraint(equalTo: superview.bottom, constant: -insets.bottom) ] } @@ -341,11 +344,14 @@ extension UIView { public func edgeConstraints( insets: CanonicalInsets ) -> [NSLayoutConstraint] { - [ - constraint(toSuperview: .left, constant: insets.left), - constraint(toSuperview: .right, constant: -insets.right), - constraint(toSuperview: .top, constant: insets.top), - constraint(toSuperview: .bottom, constant: -insets.bottom) + assert(superview != nil, "edgeConstraints(insets:) requires superview") + guard let superview: UIView + else { return [] } + return [ + left.constraint(equalTo: superview.left, constant: insets.left), + right.constraint(equalTo: superview.right, constant: -insets.right), + top.constraint(equalTo: superview.top, constant: insets.top), + bottom.constraint(equalTo: superview.bottom, constant: -insets.bottom) ] } @@ -358,9 +364,12 @@ extension UIView { public func sideEdgeConstraints( inset: CGFloat = 0 ) -> [NSLayoutConstraint] { - [ - constraint(toSuperview: .left, constant: inset), - constraint(toSuperview: .right, constant: -inset) + assert(superview != nil, "sideEdgeConstraints(inset:) requires superview") + guard let superview: UIView + else { return [] } + return [ + left.constraint(equalTo: superview.left, constant: inset), + right.constraint(equalTo: superview.right, constant: -inset) ] } } diff --git a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift index 84797ccf..94efd8af 100644 --- a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift +++ b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift @@ -745,34 +745,10 @@ final class UIViewAutoLayoutTests: XCTestCase { // THEN expect(constraints.count) == 4 - expect(constraints[0]).to(match(NSLayoutConstraint(item: view, - attribute: .left, - relatedBy: .equal, - toItem: superview, - attribute: .left, - multiplier: 1, - constant: 0))) - expect(constraints[1]).to(match(NSLayoutConstraint(item: view, - attribute: .right, - relatedBy: .equal, - toItem: superview, - attribute: .right, - multiplier: 1, - constant: 0))) - expect(constraints[2]).to(match(NSLayoutConstraint(item: view, - attribute: .top, - relatedBy: .equal, - toItem: superview, - attribute: .top, - multiplier: 1, - constant: 0))) - expect(constraints[3]).to(match(NSLayoutConstraint(item: view, - attribute: .bottom, - relatedBy: .equal, - toItem: superview, - attribute: .bottom, - multiplier: 1, - constant: 0))) + expect(constraints[0]).to(match(view.left.constraint(to: superview.left))) + expect(constraints[1]).to(match(view.right.constraint(to: superview.right))) + expect(constraints[2]).to(match(view.top.constraint(to: superview.top))) + expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom))) } func testEdgeConstraintsInset_givenInset() { @@ -791,34 +767,10 @@ final class UIViewAutoLayoutTests: XCTestCase { // THEN expect(constraints.count) == 4 - expect(constraints[0]).to(match(NSLayoutConstraint(item: view, - attribute: .left, - relatedBy: .equal, - toItem: superview, - attribute: .left, - multiplier: 1, - constant: inset))) - expect(constraints[1]).to(match(NSLayoutConstraint(item: view, - attribute: .right, - relatedBy: .equal, - toItem: superview, - attribute: .right, - multiplier: 1, - constant: -inset))) - expect(constraints[2]).to(match(NSLayoutConstraint(item: view, - attribute: .top, - relatedBy: .equal, - toItem: superview, - attribute: .top, - multiplier: 1, - constant: inset))) - expect(constraints[3]).to(match(NSLayoutConstraint(item: view, - attribute: .bottom, - relatedBy: .equal, - toItem: superview, - attribute: .bottom, - multiplier: 1, - constant: -inset))) + expect(constraints[0]).to(match(view.left.constraint(to: superview.left, constant: inset))) + expect(constraints[1]).to(match(view.right.constraint(to: superview.right, constant: -inset))) + expect(constraints[2]).to(match(view.top.constraint(to: superview.top, constant: inset))) + expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom, constant: -inset))) } func testEdgeConstraintsInsetsDirectional() { @@ -837,34 +789,10 @@ final class UIViewAutoLayoutTests: XCTestCase { // THEN expect(constraints.count) == 4 - expect(constraints[0]).to(match(NSLayoutConstraint(item: view, - attribute: .leading, - relatedBy: .equal, - toItem: superview, - attribute: .leading, - multiplier: 1, - constant: insets.leading))) - expect(constraints[1]).to(match(NSLayoutConstraint(item: view, - attribute: .trailing, - relatedBy: .equal, - toItem: superview, - attribute: .trailing, - multiplier: 1, - constant: -insets.trailing))) - expect(constraints[2]).to(match(NSLayoutConstraint(item: view, - attribute: .top, - relatedBy: .equal, - toItem: superview, - attribute: .top, - multiplier: 1, - constant: insets.top))) - expect(constraints[3]).to(match(NSLayoutConstraint(item: view, - attribute: .bottom, - relatedBy: .equal, - toItem: superview, - attribute: .bottom, - multiplier: 1, - constant: -insets.bottom))) + expect(constraints[0]).to(match(view.leading.constraint(to: superview.leading, constant: insets.leading))) + expect(constraints[1]).to(match(view.trailing.constraint(to: superview.trailing, constant: -insets.trailing))) + expect(constraints[2]).to(match(view.top.constraint(to: superview.top, constant: insets.top))) + expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom, constant: -insets.bottom))) } func testEdgeConstraintsInsetsCanonical() { @@ -883,34 +811,10 @@ final class UIViewAutoLayoutTests: XCTestCase { // THEN expect(constraints.count) == 4 - expect(constraints[0]).to(match(NSLayoutConstraint(item: view, - attribute: .left, - relatedBy: .equal, - toItem: superview, - attribute: .left, - multiplier: 1, - constant: insets.left))) - expect(constraints[1]).to(match(NSLayoutConstraint(item: view, - attribute: .right, - relatedBy: .equal, - toItem: superview, - attribute: .right, - multiplier: 1, - constant: -insets.right))) - expect(constraints[2]).to(match(NSLayoutConstraint(item: view, - attribute: .top, - relatedBy: .equal, - toItem: superview, - attribute: .top, - multiplier: 1, - constant: insets.top))) - expect(constraints[3]).to(match(NSLayoutConstraint(item: view, - attribute: .bottom, - relatedBy: .equal, - toItem: superview, - attribute: .bottom, - multiplier: 1, - constant: -insets.bottom))) + expect(constraints[0]).to(match(view.left.constraint(to: superview.left, constant: insets.left))) + expect(constraints[1]).to(match(view.right.constraint(to: superview.right, constant: -insets.right))) + expect(constraints[2]).to(match(view.top.constraint(to: superview.top, constant: insets.top))) + expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom, constant: -insets.bottom))) } func testSideEdgeConstraintsInset() { @@ -928,20 +832,8 @@ final class UIViewAutoLayoutTests: XCTestCase { // THEN expect(constraints.count) == 2 - expect(constraints[0]).to(match(NSLayoutConstraint(item: view, - attribute: .left, - relatedBy: .equal, - toItem: superview, - attribute: .left, - multiplier: 1, - constant: 0))) - expect(constraints[1]).to(match(NSLayoutConstraint(item: view, - attribute: .right, - relatedBy: .equal, - toItem: superview, - attribute: .right, - multiplier: 1, - constant: 0))) + expect(constraints[0]).to(match(view.left.constraint(to: superview.left))) + expect(constraints[1]).to(match(view.right.constraint(to: superview.right))) } func testSideEdgeConstraintsInset_givenInset() { @@ -960,19 +852,7 @@ final class UIViewAutoLayoutTests: XCTestCase { // THEN expect(constraints.count) == 2 - expect(constraints[0]).to(match(NSLayoutConstraint(item: view, - attribute: .left, - relatedBy: .equal, - toItem: superview, - attribute: .left, - multiplier: 1, - constant: inset))) - expect(constraints[1]).to(match(NSLayoutConstraint(item: view, - attribute: .right, - relatedBy: .equal, - toItem: superview, - attribute: .right, - multiplier: 1, - constant: -inset))) + expect(constraints[0]).to(match(view.left.constraint(to: superview.left, constant: inset))) + expect(constraints[1]).to(match(view.right.constraint(to: superview.right, constant: -inset))) } }