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

Add safe area constraints methods #356

Merged
merged 1 commit into from
Mar 2, 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
53 changes: 53 additions & 0 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,57 @@ extension UIView {
right.constraint(to: superview.margins.right, constant: -inset)
]
}

// MARK: - Safe Area

/// Creates constraints aligning the edges of the receiver to the safe area of the superview with an inset.
///
/// - Parameter inset: The inset value.
///
/// - Returns: The created constraints.
public func safeAreaConstraints(
inset: CGFloat = 0
) -> [NSLayoutConstraint] {
safeAreaConstraints(insets: UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset))
}

/// Creates constraints aligning the edges of the receiver to the safe area of the superview with directional insets
/// ([`NSDirectionalEdgeInsets`](https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets)).
///
/// - Parameter insets: The directional insets.
///
/// - Returns: The created constraints.
public func safeAreaConstraints(
insets: DirectionalInsets
) -> [NSLayoutConstraint] {
assert(superview != nil, "safeAreaConstraints(insets:) requires superview")
guard let superview: UIView
else { return [] }
return [
leading.constraint(to: superview.safeArea.leading, constant: insets.leading),
trailing.constraint(to: superview.safeArea.trailing, constant: -insets.trailing),
top.constraint(to: superview.safeArea.top, constant: insets.top),
bottom.constraint(to: superview.safeArea.bottom, constant: -insets.bottom)
]
}

/// Creates constraints aligning the edges of the receiver to the safe area of the superview with canonical insets
/// ([`UIEdgeInsets`](https://developer.apple.com/documentation/uikit/uiedgeinsets)).
///
/// - Parameter insets: The canonical insets.
///
/// - Returns: The created constraints.
public func safeAreaConstraints(
insets: CanonicalInsets
) -> [NSLayoutConstraint] {
assert(superview != nil, "safeAreaConstraints(insets:) requires superview")
guard let superview: UIView
else { return [] }
return [
left.constraint(to: superview.safeArea.left, constant: insets.left),
right.constraint(to: superview.safeArea.right, constant: -insets.right),
top.constraint(to: superview.safeArea.top, constant: insets.top),
bottom.constraint(to: superview.safeArea.bottom, constant: -insets.bottom)
]
}
}
97 changes: 97 additions & 0 deletions Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -987,4 +987,101 @@ final class UIViewAutoLayoutTests: XCTestCase {
expect(constraints[0]).to(match(view.left.constraint(to: superview.margins.left, constant: inset)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.margins.right, constant: -inset)))
}

// MARK: - Safe Area

func testSafeAreaConstraintsInset() {

// GIVEN

let superview: UIView = .init()
let view: UIView = .init()
superview.addSubview(view)

// WHEN

let constraints: [NSLayoutConstraint] = view.safeAreaConstraints()

// THEN

expect(constraints.count) == 4
expect(constraints[0]).to(match(view.left.constraint(to: superview.safeArea.left)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.safeArea.right)))
expect(constraints[2]).to(match(view.top.constraint(to: superview.safeArea.top)))
expect(constraints[3]).to(match(view.bottom.constraint(to: superview.safeArea.bottom)))
}

func testSafeAreaConstraintsInset_givenInset() {

// GIVEN

let superview: UIView = .init()
let view: UIView = .init()
superview.addSubview(view)
let inset: CGFloat = 10

// WHEN

let constraints: [NSLayoutConstraint] = view.safeAreaConstraints(inset: inset)

// THEN

expect(constraints.count) == 4
expect(constraints[0]).to(match(view.left.constraint(to: superview.safeArea.left, constant: inset)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.safeArea.right, constant: -inset)))
expect(constraints[2]).to(match(view.top.constraint(to: superview.safeArea.top, constant: inset)))
expect(constraints[3]).to(match(view.bottom.constraint(to: superview.safeArea.bottom, constant: -inset)))
}

func testSafeAreaConstraintsInsetsDirectional() {

// GIVEN

let superview: UIView = .init()
let view: UIView = .init()
superview.addSubview(view)
let insets: NSDirectionalEdgeInsets = .init(top: 1, leading: 2, bottom: 3, trailing: 4)

// WHEN

let constraints: [NSLayoutConstraint] = view.safeAreaConstraints(insets: insets)

// THEN

expect(constraints.count) == 4
expect(constraints[0])
.to(match(view.leading.constraint(to: superview.safeArea.leading, constant: insets.leading)))
expect(constraints[1])
.to(match(view.trailing.constraint(to: superview.safeArea.trailing, constant: -insets.trailing)))
expect(constraints[2])
.to(match(view.top.constraint(to: superview.safeArea.top, constant: insets.top)))
expect(constraints[3])
.to(match(view.bottom.constraint(to: superview.safeArea.bottom, constant: -insets.bottom)))
}

func testSafeAreaConstraintsInsetsCanonical() {

// GIVEN

let superview: UIView = .init()
let view: UIView = .init()
superview.addSubview(view)
let insets: UIEdgeInsets = .init(top: 1, left: 2, bottom: 3, right: 4)

// WHEN

let constraints: [NSLayoutConstraint] = view.safeAreaConstraints(insets: insets)

// THEN

expect(constraints.count) == 4
expect(constraints[0])
.to(match(view.left.constraint(to: superview.safeArea.left, constant: insets.left)))
expect(constraints[1])
.to(match(view.right.constraint(to: superview.safeArea.right, constant: -insets.right)))
expect(constraints[2])
.to(match(view.top.constraint(to: superview.safeArea.top, constant: insets.top)))
expect(constraints[3])
.to(match(view.bottom.constraint(to: superview.safeArea.bottom, constant: -insets.bottom)))
}
}
6 changes: 6 additions & 0 deletions cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ <h3>Margins</h3>
<pre>marginConstraints(insets: canonical)</pre>
<pre>sideMarginConstraints()</pre>
<pre>sideMarginConstraints(inset: 100)</pre>
<h3>Safe Area</h3>
<p>Constrains to superview safe area.<p>
<pre>safeAreaConstraints()</pre>
<pre>safeAreaConstraints(inset: 100)</pre>
<pre>safeAreaConstraints(insets: directional)</pre>
<pre>safeAreaConstraints(insets: canonical)</pre>
<h2>Auto Layout</h2>
<h3>NSLayoutConstraint</h3>
<pre>activate()</pre>
Expand Down
Loading