Skip to content

Commit

Permalink
Use anchors to define edge constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller committed Feb 19, 2024
1 parent 2a2d4c6 commit 380a13b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 153 deletions.
35 changes: 22 additions & 13 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,14 @@ extension UIView {
public func edgeConstraints(
insets: DirectionalInsets
) -> [NSLayoutConstraint] {
[
constraint(toSuperview: .leading, constant: insets.leading),
constraint(toSuperview: .trailing, constant: -insets.trailing),
constraint(toSuperview: .top, constant: insets.top),
constraint(toSuperview: .bottom, constant: -insets.bottom)
assert(superview != nil, "edgeConstraints(insets:) requires superview")
guard let superview: UIView
else { return [] }
return [
leading.constraint(equalTo: superview.leading, constant: insets.leading),
trailing.constraint(equalTo: superview.trailing, constant: -insets.trailing),
top.constraint(equalTo: superview.top, constant: insets.top),
bottom.constraint(equalTo: superview.bottom, constant: -insets.bottom)
]
}

Expand All @@ -341,11 +344,14 @@ extension UIView {
public func edgeConstraints(
insets: CanonicalInsets
) -> [NSLayoutConstraint] {
[
constraint(toSuperview: .left, constant: insets.left),
constraint(toSuperview: .right, constant: -insets.right),
constraint(toSuperview: .top, constant: insets.top),
constraint(toSuperview: .bottom, constant: -insets.bottom)
assert(superview != nil, "edgeConstraints(insets:) requires superview")
guard let superview: UIView
else { return [] }
return [
left.constraint(equalTo: superview.left, constant: insets.left),
right.constraint(equalTo: superview.right, constant: -insets.right),
top.constraint(equalTo: superview.top, constant: insets.top),
bottom.constraint(equalTo: superview.bottom, constant: -insets.bottom)
]
}

Expand All @@ -358,9 +364,12 @@ extension UIView {
public func sideEdgeConstraints(
inset: CGFloat = 0
) -> [NSLayoutConstraint] {
[
constraint(toSuperview: .left, constant: inset),
constraint(toSuperview: .right, constant: -inset)
assert(superview != nil, "sideEdgeConstraints(inset:) requires superview")
guard let superview: UIView
else { return [] }
return [
left.constraint(equalTo: superview.left, constant: inset),
right.constraint(equalTo: superview.right, constant: -inset)
]
}
}
160 changes: 20 additions & 140 deletions Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -745,34 +745,10 @@ final class UIViewAutoLayoutTests: XCTestCase {
// THEN

expect(constraints.count) == 4
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)))
expect(constraints[2]).to(match(NSLayoutConstraint(item: view,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .top,
multiplier: 1,
constant: 0)))
expect(constraints[3]).to(match(NSLayoutConstraint(item: view,
attribute: .bottom,
relatedBy: .equal,
toItem: superview,
attribute: .bottom,
multiplier: 1,
constant: 0)))
expect(constraints[0]).to(match(view.left.constraint(to: superview.left)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.right)))
expect(constraints[2]).to(match(view.top.constraint(to: superview.top)))
expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom)))
}

func testEdgeConstraintsInset_givenInset() {
Expand All @@ -791,34 +767,10 @@ final class UIViewAutoLayoutTests: XCTestCase {
// THEN

expect(constraints.count) == 4
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)))
expect(constraints[2]).to(match(NSLayoutConstraint(item: view,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .top,
multiplier: 1,
constant: inset)))
expect(constraints[3]).to(match(NSLayoutConstraint(item: view,
attribute: .bottom,
relatedBy: .equal,
toItem: superview,
attribute: .bottom,
multiplier: 1,
constant: -inset)))
expect(constraints[0]).to(match(view.left.constraint(to: superview.left, constant: inset)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.right, constant: -inset)))
expect(constraints[2]).to(match(view.top.constraint(to: superview.top, constant: inset)))
expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom, constant: -inset)))
}

func testEdgeConstraintsInsetsDirectional() {
Expand All @@ -837,34 +789,10 @@ final class UIViewAutoLayoutTests: XCTestCase {
// THEN

expect(constraints.count) == 4
expect(constraints[0]).to(match(NSLayoutConstraint(item: view,
attribute: .leading,
relatedBy: .equal,
toItem: superview,
attribute: .leading,
multiplier: 1,
constant: insets.leading)))
expect(constraints[1]).to(match(NSLayoutConstraint(item: view,
attribute: .trailing,
relatedBy: .equal,
toItem: superview,
attribute: .trailing,
multiplier: 1,
constant: -insets.trailing)))
expect(constraints[2]).to(match(NSLayoutConstraint(item: view,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .top,
multiplier: 1,
constant: insets.top)))
expect(constraints[3]).to(match(NSLayoutConstraint(item: view,
attribute: .bottom,
relatedBy: .equal,
toItem: superview,
attribute: .bottom,
multiplier: 1,
constant: -insets.bottom)))
expect(constraints[0]).to(match(view.leading.constraint(to: superview.leading, constant: insets.leading)))
expect(constraints[1]).to(match(view.trailing.constraint(to: superview.trailing, constant: -insets.trailing)))
expect(constraints[2]).to(match(view.top.constraint(to: superview.top, constant: insets.top)))
expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom, constant: -insets.bottom)))
}

func testEdgeConstraintsInsetsCanonical() {
Expand All @@ -883,34 +811,10 @@ final class UIViewAutoLayoutTests: XCTestCase {
// THEN

expect(constraints.count) == 4
expect(constraints[0]).to(match(NSLayoutConstraint(item: view,
attribute: .left,
relatedBy: .equal,
toItem: superview,
attribute: .left,
multiplier: 1,
constant: insets.left)))
expect(constraints[1]).to(match(NSLayoutConstraint(item: view,
attribute: .right,
relatedBy: .equal,
toItem: superview,
attribute: .right,
multiplier: 1,
constant: -insets.right)))
expect(constraints[2]).to(match(NSLayoutConstraint(item: view,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .top,
multiplier: 1,
constant: insets.top)))
expect(constraints[3]).to(match(NSLayoutConstraint(item: view,
attribute: .bottom,
relatedBy: .equal,
toItem: superview,
attribute: .bottom,
multiplier: 1,
constant: -insets.bottom)))
expect(constraints[0]).to(match(view.left.constraint(to: superview.left, constant: insets.left)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.right, constant: -insets.right)))
expect(constraints[2]).to(match(view.top.constraint(to: superview.top, constant: insets.top)))
expect(constraints[3]).to(match(view.bottom.constraint(to: superview.bottom, constant: -insets.bottom)))
}

func testSideEdgeConstraintsInset() {
Expand All @@ -928,20 +832,8 @@ final class UIViewAutoLayoutTests: XCTestCase {
// 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)))
expect(constraints[0]).to(match(view.left.constraint(to: superview.left)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.right)))
}

func testSideEdgeConstraintsInset_givenInset() {
Expand All @@ -960,19 +852,7 @@ final class UIViewAutoLayoutTests: XCTestCase {
// 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)))
expect(constraints[0]).to(match(view.left.constraint(to: superview.left, constant: inset)))
expect(constraints[1]).to(match(view.right.constraint(to: superview.right, constant: -inset)))
}
}

0 comments on commit 380a13b

Please sign in to comment.