Skip to content

Commit

Permalink
Reorder inset method to be before insets methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller committed Feb 1, 2024
1 parent b053325 commit 727ab63
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
40 changes: 20 additions & 20 deletions Sources/Layout/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,72 +320,72 @@ public final class Layout { // swiftlint:disable:this type_body_length
return adding(constraint.withPriority(priority))
}

/// Adds constraints aligning the edges of the given view and target view with directional insets
/// ([`NSDirectionalEdgeInsets`](https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets)).
/// Adds constraints aligning the edges of the given view and target view with an inset.
///
/// - Note: The `view` will be inset from the `targetView`.
///
/// - Parameters:
/// - view: The view to constrain to the `targetView`.
/// - targetView: The target view to which to constrain the `view`.
/// - insets: The directional insets.
/// - inset: The inset value.
///
/// - Returns: The layout instance with the added constraints.
@discardableResult
public func constrain(
_ view: UIView,
to targetView: UIView,
insets: DirectionalInsets
inset: CGFloat = 0
) -> Layout {
self
.constrain(view.leading, to: targetView.leading, constant: insets.leading)
.constrain(view.trailing, to: targetView.trailing, constant: -insets.trailing)
.constrain(view.top, to: targetView.top, constant: insets.top)
.constrain(view.bottom, to: targetView.bottom, constant: -insets.bottom)
let insets: UIEdgeInsets = .init(top: inset, left: inset, bottom: inset, right: inset)
return constrain(view, to: targetView, insets: insets)
}

/// Adds constraints aligning the edges of the given view and target view with canonical insets
/// ([`UIEdgeInsets`](https://developer.apple.com/documentation/uikit/uiedgeinsets)).
/// Adds constraints aligning the edges of the given view and target view with directional insets
/// ([`NSDirectionalEdgeInsets`](https://developer.apple.com/documentation/uikit/nsdirectionaledgeinsets)).
///
/// - Note: The `view` will be inset from the `targetView`.
///
/// - Parameters:
/// - view: The view to constrain to the `targetView`.
/// - targetView: The target view to which to constrain the `view`.
/// - insets: The canonical insets.
/// - insets: The directional insets.
///
/// - Returns: The layout instance with the added constraints.
@discardableResult
public func constrain(
_ view: UIView,
to targetView: UIView,
insets: CanonicalInsets
insets: DirectionalInsets
) -> Layout {
self
.constrain(view.left, to: targetView.left, constant: insets.left)
.constrain(view.right, to: targetView.right, constant: -insets.right)
.constrain(view.leading, to: targetView.leading, constant: insets.leading)
.constrain(view.trailing, to: targetView.trailing, constant: -insets.trailing)
.constrain(view.top, to: targetView.top, constant: insets.top)
.constrain(view.bottom, to: targetView.bottom, constant: -insets.bottom)
}

/// Adds constraints aligning the edges of the given view and target view with an inset.
/// Adds constraints aligning the edges of the given view and target view with canonical insets
/// ([`UIEdgeInsets`](https://developer.apple.com/documentation/uikit/uiedgeinsets)).
///
/// - Note: The `view` will be inset from the `targetView`.
///
/// - Parameters:
/// - view: The view to constrain to the `targetView`.
/// - targetView: The target view to which to constrain the `view`.
/// - inset: The inset value.
/// - insets: The canonical insets.
///
/// - Returns: The layout instance with the added constraints.
@discardableResult
public func constrain(
_ view: UIView,
to targetView: UIView,
inset: CGFloat = 0
insets: CanonicalInsets
) -> Layout {
let insets: UIEdgeInsets = .init(top: inset, left: inset, bottom: inset, right: inset)
return constrain(view, to: targetView, insets: insets)
self
.constrain(view.left, to: targetView.left, constant: insets.left)
.constrain(view.right, to: targetView.right, constant: -insets.right)
.constrain(view.top, to: targetView.top, constant: insets.top)
.constrain(view.bottom, to: targetView.bottom, constant: -insets.bottom)
}

// swiftlint:enable function_default_parameter_at_end
Expand Down
62 changes: 31 additions & 31 deletions Tests/LayoutTests/LayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,37 @@ final class LayoutTests: XCTestCase {
}
}

func testConstrainViewToTargetViewInset() {

// GIVEN

let blueView: UIView = blueView
let pinkView: UIView = pinkView
let yellowView: UIView = yellowView

// THEN

assertLayout { view in

let layout: Layout = view.layout {
blueView
.toEdges(inset: 20)
pinkView
yellowView
}

// Constrain with Default Inset

layout.constrain(pinkView, to: blueView)

// Constrain with Inset

layout.constrain(yellowView, to: blueView, inset: 20)

layout.activate()
}
}

func testConstrainViewToTargetViewInsetsDirectional() {

// GIVEN
Expand Down Expand Up @@ -579,37 +610,6 @@ final class LayoutTests: XCTestCase {
}
}

func testConstrainViewToTargetViewInset() {

// GIVEN

let blueView: UIView = blueView
let pinkView: UIView = pinkView
let yellowView: UIView = yellowView

// THEN

assertLayout { view in

let layout: Layout = view.layout {
blueView
.toEdges(inset: 20)
pinkView
yellowView
}

// Constrain with Default Inset

layout.constrain(pinkView, to: blueView)

// Constrain with Inset

layout.constrain(yellowView, to: blueView, inset: 20)

layout.activate()
}
}

// MARK: - Equal

func testEqualAttributeWithViews() {
Expand Down
2 changes: 1 addition & 1 deletion cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ <h3>Constrain</h3>
is: .greaterThanOrEqual,
to: 100,
priority: .high)</pre>
<pre>constrain(viewA, to: viewB, inset: 100)</pre>
<pre>constrain(viewA,
to: viewB,
insets: directional)</pre>
<pre>constrain(viewA,
to: viewB,
insets: canonical)</pre>
<pre>constrain(viewA, to: viewB, inset: 100)</pre>
<h3>Equal</h3>
<pre>equal(.top, views)</pre>
<pre>equal([.top, .bottom], views)</pre>
Expand Down

0 comments on commit 727ab63

Please sign in to comment.