Skip to content

Commit

Permalink
Improve UIView edge constraints methods (#339)
Browse files Browse the repository at this point in the history
* Improve `UIView` edge constraints methods

* Remove line breaks
  • Loading branch information
tinder-cfuller authored Feb 15, 2024
1 parent 2608a69 commit 73ae0c1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,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.
///
Expand All @@ -315,7 +315,8 @@ 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.
///
Expand All @@ -331,7 +332,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.
///
Expand All @@ -346,4 +348,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)
]
}
}
63 changes: 63 additions & 0 deletions Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,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)))
}
}
2 changes: 2 additions & 0 deletions cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ <h3>Edges</h3>
<pre>edgeConstraints(inset: 100)</pre>
<pre>edgeConstraints(insets: directional)</pre>
<pre>edgeConstraints(insets: canonical)</pre>
<pre>sideEdgeConstraints()</pre>
<pre>sideEdgeConstraints(inset: 100)</pre>
<h2>Auto Layout</h2>
<h3>NSLayoutConstraint</h3>
<pre>activate()</pre>
Expand Down

0 comments on commit 73ae0c1

Please sign in to comment.