From cf026aa1657254fc95f9bb461036de9f70ed1d09 Mon Sep 17 00:00:00 2001 From: Christopher Fuller Date: Tue, 13 Feb 2024 19:53:42 -0800 Subject: [PATCH 1/2] Improve `UIView` edge constraints methods --- Sources/Layout/UIKit/UIView+AutoLayout.swift | 25 +++++++- .../UIKit/UIView+AutoLayoutTests.swift | 63 +++++++++++++++++++ cheatsheet.html | 2 + 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/Sources/Layout/UIKit/UIView+AutoLayout.swift b/Sources/Layout/UIKit/UIView+AutoLayout.swift index 27366a9b..1b3acef8 100644 --- a/Sources/Layout/UIKit/UIView+AutoLayout.swift +++ b/Sources/Layout/UIKit/UIView+AutoLayout.swift @@ -294,7 +294,7 @@ extension UIView { // MARK: - Edges - /// Creates constraints to the edges of the superview of the receiver with an inset. + /// Creates constraints aligning the edges of the receiver to the edges of the superview with an inset. /// /// - Parameter inset: The inset value. /// @@ -305,7 +305,10 @@ extension UIView { edgeConstraints(insets: UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)) } - /// Creates constraints to the directional edges of the superview of the receiver with insets. + /// Creates constraints aligning the edges of the receiver to the edges of the superview with directional insets + /// ([`NSDirectionalEdgeInsets`]( + /// https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets + /// )). /// /// - Parameter insets: The directional insets. /// @@ -321,7 +324,8 @@ extension UIView { ] } - /// Creates constraints to the canonical edges of the superview of the receiver with insets. + /// Creates constraints aligning the edges of the receiver to the edges of the superview with canonical insets + /// ([`UIEdgeInsets`](https://developer.apple.com/documentation/uikit/uiedgeinsets)). /// /// - Parameter insets: The canonical insets. /// @@ -336,4 +340,19 @@ extension UIView { constraint(toSuperview: .bottom, constant: -insets.bottom) ] } + + /// Creates constraints aligning the left and right edges of the receiver to the corresponding edges of the + /// superview with an inset. + /// + /// - Parameter inset: The inset value. + /// + /// - Returns: The created constraints. + public func sideEdgeConstraints( + inset: CGFloat = 0 + ) -> [NSLayoutConstraint] { + [ + constraint(toSuperview: .left, constant: inset), + constraint(toSuperview: .right, constant: -inset) + ] + } } diff --git a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift index 7000c37a..eb0aa55e 100644 --- a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift +++ b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift @@ -883,4 +883,67 @@ final class UIViewAutoLayoutTests: XCTestCase { multiplier: 1, constant: -insets.bottom))) } + + func testSideEdgeConstraintsInset() { + + // GIVEN + + let superview: UIView = .init() + let view: UIView = .init() + superview.addSubview(view) + + // WHEN + + let constraints: [NSLayoutConstraint] = view.sideEdgeConstraints() + + // 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))) + } + + func testSideEdgeConstraintsInset_givenInset() { + + // GIVEN + + let superview: UIView = .init() + let view: UIView = .init() + superview.addSubview(view) + let inset: CGFloat = 10 + + // WHEN + + let constraints: [NSLayoutConstraint] = view.sideEdgeConstraints(inset: inset) + + // 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))) + } } diff --git a/cheatsheet.html b/cheatsheet.html index ea056e34..9781cd9b 100644 --- a/cheatsheet.html +++ b/cheatsheet.html @@ -331,6 +331,8 @@

Edges

edgeConstraints(inset: 100)
edgeConstraints(insets: directional)
edgeConstraints(insets: canonical)
+
sideEdgeConstraints()
+
sideEdgeConstraints(inset: 100)

Auto Layout

NSLayoutConstraint

activate()
From c6b839e4c7d21b5a154279b3869bc4ab87efe431 Mon Sep 17 00:00:00 2001 From: Christopher Fuller Date: Wed, 14 Feb 2024 16:07:31 -0800 Subject: [PATCH 2/2] Remove line breaks --- Sources/Layout/UIKit/UIView+AutoLayout.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/Layout/UIKit/UIView+AutoLayout.swift b/Sources/Layout/UIKit/UIView+AutoLayout.swift index 1b3acef8..adecc2d4 100644 --- a/Sources/Layout/UIKit/UIView+AutoLayout.swift +++ b/Sources/Layout/UIKit/UIView+AutoLayout.swift @@ -306,9 +306,7 @@ extension UIView { } /// Creates constraints aligning the edges of the receiver to the edges of the superview with directional insets - /// ([`NSDirectionalEdgeInsets`]( - /// https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets - /// )). + /// ([`NSDirectionalEdgeInsets`](https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets)). /// /// - Parameter insets: The directional insets. ///