From 380a13b327109b8b447e7f09aa9c117310dfb0cb Mon Sep 17 00:00:00 2001 From: Christopher Fuller Date: Mon, 19 Feb 2024 12:25:19 -0800 Subject: [PATCH 1/2] Use anchors to define edge constraints --- Sources/Layout/UIKit/UIView+AutoLayout.swift | 35 ++-- .../UIKit/UIView+AutoLayoutTests.swift | 160 +++--------------- 2 files changed, 42 insertions(+), 153 deletions(-) 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))) } } From afa79a583154c20df49d6c3ab3337c872787a879 Mon Sep 17 00:00:00 2001 From: Christopher Fuller Date: Mon, 19 Feb 2024 18:14:53 -0800 Subject: [PATCH 2/2] Use `to:` argument label --- Sources/Layout/UIKit/UIView+AutoLayout.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/Layout/UIKit/UIView+AutoLayout.swift b/Sources/Layout/UIKit/UIView+AutoLayout.swift index 7de90412..e75237df 100644 --- a/Sources/Layout/UIKit/UIView+AutoLayout.swift +++ b/Sources/Layout/UIKit/UIView+AutoLayout.swift @@ -328,10 +328,10 @@ extension UIView { 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) + leading.constraint(to: superview.leading, constant: insets.leading), + trailing.constraint(to: superview.trailing, constant: -insets.trailing), + top.constraint(to: superview.top, constant: insets.top), + bottom.constraint(to: superview.bottom, constant: -insets.bottom) ] } @@ -348,10 +348,10 @@ extension UIView { 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) + left.constraint(to: superview.left, constant: insets.left), + right.constraint(to: superview.right, constant: -insets.right), + top.constraint(to: superview.top, constant: insets.top), + bottom.constraint(to: superview.bottom, constant: -insets.bottom) ] } @@ -368,8 +368,8 @@ extension UIView { guard let superview: UIView else { return [] } return [ - left.constraint(equalTo: superview.left, constant: inset), - right.constraint(equalTo: superview.right, constant: -inset) + left.constraint(to: superview.left, constant: inset), + right.constraint(to: superview.right, constant: -inset) ] } }