Skip to content

Commit

Permalink
Fix to() method by using canonical attribute (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller authored Oct 13, 2023
1 parent ea56eb8 commit 10e9150
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 51 deletions.
15 changes: 9 additions & 6 deletions Sources/Layout/LayoutItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ extension LayoutItem {
) -> LayoutItem {
addingSuperviewConstraints {
$0.layoutItemView
.constraint(is: relation,
.constraint(for: attribute.canonicalAttribute,
is: relation,
toSuperview: attribute,
multiplier: multiplier,
constant: constant)
Expand All @@ -312,11 +313,13 @@ extension LayoutItem {
_ constant: CGFloat = 0,
priority: UILayoutPriority = .required
) -> LayoutItem {
addingSuperviewConstraints {
$0.layoutItemView
.constraints(toSuperview: attributes,
constant: constant)
.withPriority(priority)
addingSuperviewConstraints { layoutItem in
for attribute: NSLayoutConstraint.Attribute in attributes {
layoutItem
.layoutItemView
.constraint(for: attribute.canonicalAttribute, toSuperview: attribute, constant: constant)
.withPriority(priority)
}
}
}

Expand Down
45 changes: 25 additions & 20 deletions Sources/Layout/NSLayoutConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,33 @@ extension NSLayoutConstraint {

extension NSLayoutConstraint.Attribute {

internal var marginAttribute: NSLayoutConstraint.Attribute {
internal var canonicalAttribute: NSLayoutConstraint.Attribute {
switch self {
case .left:
return .leftMargin
case .right:
return .rightMargin
case .top:
return .topMargin
case .bottom:
return .bottomMargin
case .leading:
return .leadingMargin
case .trailing:
return .trailingMargin
case .centerX:
return .centerXWithinMargins
case .centerY:
return .centerYWithinMargins
case .leftMargin, .rightMargin, .topMargin, .bottomMargin,
.leadingMargin, .trailingMargin, .centerXWithinMargins, .centerYWithinMargins:
case .leftMargin:
return .left
case .rightMargin:
return .right
case .topMargin:
return .top
case .bottomMargin:
return .bottom
case .leadingMargin:
return .leading
case .trailingMargin:
return .trailing
case .centerXWithinMargins:
return .centerX
case .centerYWithinMargins:
return .centerY
case .left, .right, .top, .bottom, .leading, .trailing:
return self
case .width, .height, .lastBaseline, .firstBaseline, .notAnAttribute:
case .centerX, .centerY:
return self
case .width, .height:
return self
case .firstBaseline, .lastBaseline:
return self
case .notAnAttribute:
return self
@unknown default:
return self
Expand Down
53 changes: 28 additions & 25 deletions Tests/LayoutTests/NSLayoutConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,34 @@ final class NSLayoutConstraintTests: XCTestCase {
expect(constraint.priority) == .high
}

func testMarginAttribute() {

expect(NSLayoutConstraint.Attribute.left.marginAttribute) == .leftMargin
expect(NSLayoutConstraint.Attribute.right.marginAttribute) == .rightMargin
expect(NSLayoutConstraint.Attribute.top.marginAttribute) == .topMargin
expect(NSLayoutConstraint.Attribute.bottom.marginAttribute) == .bottomMargin
expect(NSLayoutConstraint.Attribute.leading.marginAttribute) == .leadingMargin
expect(NSLayoutConstraint.Attribute.trailing.marginAttribute) == .trailingMargin

expect(NSLayoutConstraint.Attribute.centerX.marginAttribute) == .centerXWithinMargins
expect(NSLayoutConstraint.Attribute.centerY.marginAttribute) == .centerYWithinMargins
expect(NSLayoutConstraint.Attribute.leftMargin.marginAttribute) == .leftMargin
expect(NSLayoutConstraint.Attribute.rightMargin.marginAttribute) == .rightMargin
expect(NSLayoutConstraint.Attribute.topMargin.marginAttribute) == .topMargin
expect(NSLayoutConstraint.Attribute.bottomMargin.marginAttribute) == .bottomMargin
expect(NSLayoutConstraint.Attribute.leadingMargin.marginAttribute) == .leadingMargin
expect(NSLayoutConstraint.Attribute.trailingMargin.marginAttribute) == .trailingMargin
expect(NSLayoutConstraint.Attribute.centerXWithinMargins.marginAttribute) == .centerXWithinMargins
expect(NSLayoutConstraint.Attribute.centerYWithinMargins.marginAttribute) == .centerYWithinMargins

expect(NSLayoutConstraint.Attribute.width.marginAttribute) == .width
expect(NSLayoutConstraint.Attribute.height.marginAttribute) == .height
expect(NSLayoutConstraint.Attribute.lastBaseline.marginAttribute) == .lastBaseline
expect(NSLayoutConstraint.Attribute.firstBaseline.marginAttribute) == .firstBaseline
expect(NSLayoutConstraint.Attribute.notAnAttribute.marginAttribute) == .notAnAttribute
func testCanonicalAttribute() {

expect(NSLayoutConstraint.Attribute.leftMargin.canonicalAttribute) == .left
expect(NSLayoutConstraint.Attribute.rightMargin.canonicalAttribute) == .right
expect(NSLayoutConstraint.Attribute.topMargin.canonicalAttribute) == .top
expect(NSLayoutConstraint.Attribute.bottomMargin.canonicalAttribute) == .bottom
expect(NSLayoutConstraint.Attribute.leadingMargin.canonicalAttribute) == .leading
expect(NSLayoutConstraint.Attribute.trailingMargin.canonicalAttribute) == .trailing
expect(NSLayoutConstraint.Attribute.centerXWithinMargins.canonicalAttribute) == .centerX
expect(NSLayoutConstraint.Attribute.centerYWithinMargins.canonicalAttribute) == .centerY

expect(NSLayoutConstraint.Attribute.left.canonicalAttribute) == .left
expect(NSLayoutConstraint.Attribute.right.canonicalAttribute) == .right
expect(NSLayoutConstraint.Attribute.top.canonicalAttribute) == .top
expect(NSLayoutConstraint.Attribute.bottom.canonicalAttribute) == .bottom
expect(NSLayoutConstraint.Attribute.leading.canonicalAttribute) == .leading
expect(NSLayoutConstraint.Attribute.trailing.canonicalAttribute) == .trailing

expect(NSLayoutConstraint.Attribute.centerX.canonicalAttribute) == .centerX
expect(NSLayoutConstraint.Attribute.centerY.canonicalAttribute) == .centerY

expect(NSLayoutConstraint.Attribute.width.canonicalAttribute) == .width
expect(NSLayoutConstraint.Attribute.height.canonicalAttribute) == .height

expect(NSLayoutConstraint.Attribute.firstBaseline.canonicalAttribute) == .firstBaseline
expect(NSLayoutConstraint.Attribute.lastBaseline.canonicalAttribute) == .lastBaseline

expect(NSLayoutConstraint.Attribute.notAnAttribute.canonicalAttribute) == .notAnAttribute
}

func testOrientation() {
Expand Down

0 comments on commit 10e9150

Please sign in to comment.