Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve UIView edge constraints methods #339

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -305,7 +305,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 @@ -321,7 +322,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 @@ -336,4 +338,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 @@ -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)))
}
}
2 changes: 2 additions & 0 deletions cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,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
Loading