Skip to content

Commit 73ae0c1

Browse files
Improve UIView edge constraints methods (#339)
* Improve `UIView` edge constraints methods * Remove line breaks
1 parent 2608a69 commit 73ae0c1

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

Sources/Layout/UIKit/UIView+AutoLayout.swift

+20-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ extension UIView {
304304

305305
// MARK: - Edges
306306

307-
/// Creates constraints to the edges of the superview of the receiver with an inset.
307+
/// Creates constraints aligning the edges of the receiver to the edges of the superview with an inset.
308308
///
309309
/// - Parameter inset: The inset value.
310310
///
@@ -315,7 +315,8 @@ extension UIView {
315315
edgeConstraints(insets: UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset))
316316
}
317317

318-
/// Creates constraints to the directional edges of the superview of the receiver with insets.
318+
/// Creates constraints aligning the edges of the receiver to the edges of the superview with directional insets
319+
/// ([`NSDirectionalEdgeInsets`](https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets)).
319320
///
320321
/// - Parameter insets: The directional insets.
321322
///
@@ -331,7 +332,8 @@ extension UIView {
331332
]
332333
}
333334

334-
/// Creates constraints to the canonical edges of the superview of the receiver with insets.
335+
/// Creates constraints aligning the edges of the receiver to the edges of the superview with canonical insets
336+
/// ([`UIEdgeInsets`](https://developer.apple.com/documentation/uikit/uiedgeinsets)).
335337
///
336338
/// - Parameter insets: The canonical insets.
337339
///
@@ -346,4 +348,19 @@ extension UIView {
346348
constraint(toSuperview: .bottom, constant: -insets.bottom)
347349
]
348350
}
351+
352+
/// Creates constraints aligning the left and right edges of the receiver to the corresponding edges of the
353+
/// superview with an inset.
354+
///
355+
/// - Parameter inset: The inset value.
356+
///
357+
/// - Returns: The created constraints.
358+
public func sideEdgeConstraints(
359+
inset: CGFloat = 0
360+
) -> [NSLayoutConstraint] {
361+
[
362+
constraint(toSuperview: .left, constant: inset),
363+
constraint(toSuperview: .right, constant: -inset)
364+
]
365+
}
349366
}

Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift

+63
Original file line numberDiff line numberDiff line change
@@ -912,4 +912,67 @@ final class UIViewAutoLayoutTests: XCTestCase {
912912
multiplier: 1,
913913
constant: -insets.bottom)))
914914
}
915+
916+
func testSideEdgeConstraintsInset() {
917+
918+
// GIVEN
919+
920+
let superview: UIView = .init()
921+
let view: UIView = .init()
922+
superview.addSubview(view)
923+
924+
// WHEN
925+
926+
let constraints: [NSLayoutConstraint] = view.sideEdgeConstraints()
927+
928+
// THEN
929+
930+
expect(constraints.count) == 2
931+
expect(constraints[0]).to(match(NSLayoutConstraint(item: view,
932+
attribute: .left,
933+
relatedBy: .equal,
934+
toItem: superview,
935+
attribute: .left,
936+
multiplier: 1,
937+
constant: 0)))
938+
expect(constraints[1]).to(match(NSLayoutConstraint(item: view,
939+
attribute: .right,
940+
relatedBy: .equal,
941+
toItem: superview,
942+
attribute: .right,
943+
multiplier: 1,
944+
constant: 0)))
945+
}
946+
947+
func testSideEdgeConstraintsInset_givenInset() {
948+
949+
// GIVEN
950+
951+
let superview: UIView = .init()
952+
let view: UIView = .init()
953+
superview.addSubview(view)
954+
let inset: CGFloat = 10
955+
956+
// WHEN
957+
958+
let constraints: [NSLayoutConstraint] = view.sideEdgeConstraints(inset: inset)
959+
960+
// THEN
961+
962+
expect(constraints.count) == 2
963+
expect(constraints[0]).to(match(NSLayoutConstraint(item: view,
964+
attribute: .left,
965+
relatedBy: .equal,
966+
toItem: superview,
967+
attribute: .left,
968+
multiplier: 1,
969+
constant: inset)))
970+
expect(constraints[1]).to(match(NSLayoutConstraint(item: view,
971+
attribute: .right,
972+
relatedBy: .equal,
973+
toItem: superview,
974+
attribute: .right,
975+
multiplier: 1,
976+
constant: -inset)))
977+
}
915978
}

cheatsheet.html

+2
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ <h3>Edges</h3>
333333
<pre>edgeConstraints(inset: 100)</pre>
334334
<pre>edgeConstraints(insets: directional)</pre>
335335
<pre>edgeConstraints(insets: canonical)</pre>
336+
<pre>sideEdgeConstraints()</pre>
337+
<pre>sideEdgeConstraints(inset: 100)</pre>
336338
<h2>Auto Layout</h2>
337339
<h3>NSLayoutConstraint</h3>
338340
<pre>activate()</pre>

0 commit comments

Comments
 (0)