Skip to content

Commit

Permalink
Remove toMargin methods (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller authored Oct 13, 2023
1 parent c178c87 commit 37e2a22
Show file tree
Hide file tree
Showing 42 changed files with 15 additions and 241 deletions.
71 changes: 14 additions & 57 deletions Sources/Layout/LayoutItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,20 @@ extension LayoutItem {
insets: NSDirectionalEdgeInsets = .zero,
priority: UILayoutPriority = .required
) -> LayoutItem {
self
.toMargin(.leading, insets.leading, priority: priority)
.toMargin(.trailing, -insets.trailing, priority: priority)
.toMargin(.top, insets.top, priority: priority)
.toMargin(.bottom, -insets.bottom, priority: priority)
addingSuperviewConstraints {
$0.layoutItemView
.constraint(for: .top, toSuperview: .topMargin, constant: insets.top)
.withPriority(priority)
$0.layoutItemView
.constraint(for: .bottom, toSuperview: .bottomMargin, constant: -insets.bottom)
.withPriority(priority)
$0.layoutItemView
.constraint(for: .leading, toSuperview: .leadingMargin, constant: insets.leading)
.withPriority(priority)
$0.layoutItemView
.constraint(for: .trailing, toSuperview: .trailingMargin, constant: -insets.trailing)
.withPriority(priority)
}
}

/// Constrains the view to the margins of the superview with an `inset`.
Expand Down Expand Up @@ -376,58 +385,6 @@ extension LayoutItem {
}
}

/// Constrains the `attribute` to the superview's corresponding `attribute` margin
///
/// - Note:
/// Equation: view.attribute = multiplier × superview.attribute + constant
/// - Parameters:
/// - attribute: attribute to constrain
/// - relation: (optional) relationship (=, ≤, ≥)
/// - multiplier: (optional) multiplier
/// - constant: (optional) constant
/// - priority: (optional) priority of constraint
public func toMargin(
_ attribute: NSLayoutConstraint.Attribute,
is relation: NSLayoutConstraint.Relation = .equal,
multiplier: CGFloat = 1,
_ constant: CGFloat = 0,
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints {
$0.layoutItemView
.constraint(for: attribute,
is: relation,
toSuperview: attribute.marginAttribute,
multiplier: multiplier,
constant: constant)
.withPriority(priority)
}
}

/// Constrains the `attributes` to the superview's corresponding `attributes` margin
///
/// - Note:
/// Equation: view.attribute = superview.attribute + constant
/// - Parameters:
/// - attributes: attribute to constrain
/// - constant: (optional) constant
/// - priority: (optional) priority of constraint
public func toMargin(
_ attributes: [NSLayoutConstraint.Attribute],
_ constant: CGFloat = 0,
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints {
for attribute in attributes {
$0.layoutItemView
.constraint(for: attribute,
toSuperview: attribute.marginAttribute,
constant: constant)
.withPriority(priority)
}
}
}

/// Constrains the `bottom` to the bottom margin with a minimum bottom inset
/// Useful for bottom-aligned elements on iPhoneX family devices that require
/// an additional bottom margin on non-iPhoneX devices.
Expand Down
110 changes: 1 addition & 109 deletions Tests/LayoutTests/LayoutItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ final class LayoutItemTests: XCTestCase {
}
}

func testToMarginWithInset_andWithPriority() {
func testToMarginsWithInset_andWithPriority() {
assertLayout { view in
view.layout {
pinkView
Expand Down Expand Up @@ -524,114 +524,6 @@ final class LayoutItemTests: XCTestCase {
}
}

func testToMargin_andWithMultiplier_andWithConstant_andWithPriority() {
assertLayout { view in
view.layout {

// To Top Leading

pinkView
.size(width: 100, height: 100)
.toMargin(.top)
.toMargin(.leading)

// To Top Leading with Constant

yellowView
.size(width: 100, height: 100)
.toMargin(.top, 100)
.toMargin(.leading, 100)

// To Top Trailing with Multiplier

blueView
.size(width: 100, height: 100)
.toMargin(.top)
.toMargin(.trailing, multiplier: 0.75)

// To Bottom Leading with Bottom Multiplier and Constant

greenView
.size(width: 100, height: 100)
.to(.bottom, multiplier: 0.75, 50)
.to(.leading)

// To Bottom Trailing with Constant and Priority

orangeView
.size(width: 100, height: 100)
.toMargin(.bottom, -100, priority: .low)
.toMargin(.bottom, -50, priority: .high)
.toMargin(.trailing, -50)
}
}
}

func testToMarginWithRelation() {
assertLayout { view in
view.layout {

// To Top Leading With Less Than Or Equal Relation and Higher Constraint

pinkView
.size(width: 100, height: 100)
.toMargin(.top, is: .lessThanOrEqual, 100)
.toMargin(.top, 150, priority: .high)
.toMargin(.leading, 50)

// To Top Trailing With Less Than Or Equal Relation and Lower Constraint

yellowView
.size(width: 100, height: 100)
.toMargin(.top, is: .lessThanOrEqual, 100)
.toMargin(.top, 50, priority: .high)
.toMargin(.trailing, -50)

// To Top Trailing With Greater Than Or Equal Relation and Lower Constraint

blueView
.size(width: 100, height: 100)
.toMargin(.bottom, is: .greaterThanOrEqual, -100)
.toMargin(.bottom, -150, priority: .high)
.toMargin(.leading, 50)

// To Top Trailing With Greater Than Or Equal Relation and Higher Constraint

greenView
.size(width: 100, height: 100)
.toMargin(.bottom, is: .greaterThanOrEqual, -100)
.toMargin(.bottom, -50, priority: .high)
.toMargin(.trailing, -50)
}
}
}

func testToMarginWithAttributes_andWithConstant_andWithPriority() {
assertLayout { view in
view.layout {

// To Bottom Leading

pinkView
.size(width: 100, height: 100)
.toMargin([.bottom, .leading])

// To Bottom Trailing with Constant

yellowView
.size(width: 100, height: 100)
.toMargin([.bottom, .trailing], -100)

// To Top Leading with Priority

blueView
.size(width: 100, height: 100)
.toMargin([.top, .leading], 25, priority: .low)
.toMargin([.top, .leading], 100, priority: .high)
}
}
}

func testToBottomMargin_andWithPriority() {
assertLayout { view in
view.layout {
Expand Down
Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

0 comments on commit 37e2a22

Please sign in to comment.