diff --git a/Sources/Layout/UIKit/NSLayoutDimension.swift b/Sources/Layout/UIKit/NSLayoutDimension.swift index 86a3fc78..8197d6fa 100644 --- a/Sources/Layout/UIKit/NSLayoutDimension.swift +++ b/Sources/Layout/UIKit/NSLayoutDimension.swift @@ -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) } } diff --git a/Tests/LayoutTests/UIKit/NSLayoutDimensionTests.swift b/Tests/LayoutTests/UIKit/NSLayoutDimensionTests.swift index 8fe5daec..a93e168e 100644 --- a/Tests/LayoutTests/UIKit/NSLayoutDimensionTests.swift +++ b/Tests/LayoutTests/UIKit/NSLayoutDimensionTests.swift @@ -14,7 +14,7 @@ import XCTest @MainActor final class NSLayoutDimensionTests: XCTestCase { - func testConstraintIsRelationToAnchorConstant_givenDefaults() { + func testConstraintIsRelationToAnchorMultiplierConstant_givenDefaults() { // GIVEN @@ -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 @@ -56,7 +77,7 @@ final class NSLayoutDimensionTests: XCTestCase { expect(constraint).to(match(expected)) } - func testConstraintIsRelationToAnchorConstant_givenGreaterThanOrEqualRelation() { + func testConstraintIsRelationToAnchorMultiplierConstant_givenGreaterThanOrEqualRelation() { // GIVEN @@ -77,7 +98,7 @@ final class NSLayoutDimensionTests: XCTestCase { expect(constraint).to(match(expected)) } - func testConstraintIsRelationToAnchorConstant_givenLessThanOrEqualRelation() { + func testConstraintIsRelationToAnchorMultiplierConstant_givenLessThanOrEqualRelation() { // GIVEN diff --git a/cheatsheet.html b/cheatsheet.html index bea4a7f2..92b6f762 100644 --- a/cheatsheet.html +++ b/cheatsheet.html @@ -354,6 +354,7 @@

NSLayoutDimension

constraint(to: view.width)
constraint(is: .greaterThanOrEqual,
            to: view.width,
+           multiplier: 2,
            constant: 100)
constraint(is: .greaterThanOrEqual,
            to: 100)