Skip to content

Commit

Permalink
Add multiplier argument to NSLayoutDimension (#350)
Browse files Browse the repository at this point in the history
* Add multiplier argument to NSLayoutDimension

* Update cheatsheet

* Add documentation
  • Loading branch information
tinder-cfuller authored Feb 23, 2024
1 parent 12f3394 commit 0a36026
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
10 changes: 6 additions & 4 deletions Sources/Layout/UIKit/NSLayoutDimension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ extension NSLayoutDimension {
/// - Parameters:
/// - relation: The relationship between the receiver and the dimension anchor.
/// - anchor: The anchor to which to constrain.
/// - multiplier: The multiplier for the constraint.
/// - constant: The offset for the constraint.
///
/// - Returns: The created constraint.
public func constraint(
is relation: NSLayoutConstraint.Relation = .equal,
to anchor: NSLayoutDimension,
multiplier: CGFloat = 1,
constant: CGFloat = 0
) -> NSLayoutConstraint {
switch relation {
case .equal:
return constraint(equalTo: anchor, constant: constant)
return constraint(equalTo: anchor, multiplier: multiplier, constant: constant)
case .greaterThanOrEqual:
return constraint(greaterThanOrEqualTo: anchor, constant: constant)
return constraint(greaterThanOrEqualTo: anchor, multiplier: multiplier, constant: constant)
case .lessThanOrEqual:
return constraint(lessThanOrEqualTo: anchor, constant: constant)
return constraint(lessThanOrEqualTo: anchor, multiplier: multiplier, constant: constant)
@unknown default:
return constraint(equalTo: anchor, constant: constant)
return constraint(equalTo: anchor, multiplier: multiplier, constant: constant)
}
}

Expand Down
29 changes: 25 additions & 4 deletions Tests/LayoutTests/UIKit/NSLayoutDimensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
@MainActor
final class NSLayoutDimensionTests: XCTestCase {

func testConstraintIsRelationToAnchorConstant_givenDefaults() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenDefaults() {

// GIVEN

Expand All @@ -35,7 +35,28 @@ final class NSLayoutDimensionTests: XCTestCase {
expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorConstant_givenConstant() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenMultiplier() {

// GIVEN

let viewA: UIView = .init()
let viewB: UIView = .init()
let expected: NSLayoutConstraint = viewA
.width
.constraint(equalTo: viewB.width, multiplier: 23)

// WHEN

let constraint: NSLayoutConstraint = viewA
.width
.constraint(to: viewB.width, multiplier: 23)

// THEN

expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorMultiplierConstant_givenConstant() {

// GIVEN

Expand All @@ -56,7 +77,7 @@ final class NSLayoutDimensionTests: XCTestCase {
expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorConstant_givenGreaterThanOrEqualRelation() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenGreaterThanOrEqualRelation() {

// GIVEN

Expand All @@ -77,7 +98,7 @@ final class NSLayoutDimensionTests: XCTestCase {
expect(constraint).to(match(expected))
}

func testConstraintIsRelationToAnchorConstant_givenLessThanOrEqualRelation() {
func testConstraintIsRelationToAnchorMultiplierConstant_givenLessThanOrEqualRelation() {

// GIVEN

Expand Down
1 change: 1 addition & 0 deletions cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ <h3>NSLayoutDimension</h3>
<pre>constraint(to: view.width)</pre>
<pre>constraint(is: .greaterThanOrEqual,
to: view.width,
multiplier: 2,
constant: 100)</pre>
<pre>constraint(is: .greaterThanOrEqual,
to: 100)</pre>
Expand Down

0 comments on commit 0a36026

Please sign in to comment.