diff --git a/Sources/Layout/LayoutItem.swift b/Sources/Layout/LayoutItem.swift index 36cb34fb..78c9a743 100644 --- a/Sources/Layout/LayoutItem.swift +++ b/Sources/Layout/LayoutItem.swift @@ -114,34 +114,35 @@ extension LayoutItem { /// Adds constraints defining the size of the ``layoutItemView``. /// /// - Parameters: - /// - width: The width value. - /// - height: The height value. + /// - size: The size value. /// - priority: The priority of the constraints. /// /// - Returns: The layout item instance with the added constraints. public func size( - width: CGFloat, - height: CGFloat, + _ size: CGSize, priority: UILayoutPriority = .required ) -> LayoutItem { addingSuperviewConstraints { layoutItem in - layoutItem.layoutItemView.widthConstraint(width).withPriority(priority) - layoutItem.layoutItemView.heightConstraint(height).withPriority(priority) + layoutItem.layoutItemView.sizeConstraints(size).withPriority(priority) } } /// Adds constraints defining the size of the ``layoutItemView``. /// /// - Parameters: - /// - size: The size value. + /// - width: The width value. + /// - height: The height value. /// - priority: The priority of the constraints. /// /// - Returns: The layout item instance with the added constraints. public func size( - _ size: CGSize, + width: CGFloat, + height: CGFloat, priority: UILayoutPriority = .required ) -> LayoutItem { - self.size(width: size.width, height: size.height, priority: priority) + addingSuperviewConstraints { layoutItem in + layoutItem.layoutItemView.sizeConstraints(width: width, height: height).withPriority(priority) + } } // MARK: - Width diff --git a/Sources/Layout/UIKit/UIView+AutoLayout.swift b/Sources/Layout/UIKit/UIView+AutoLayout.swift index 4a21b577..97fb47ed 100644 --- a/Sources/Layout/UIKit/UIView+AutoLayout.swift +++ b/Sources/Layout/UIKit/UIView+AutoLayout.swift @@ -72,6 +72,20 @@ extension UIView { ] } + /// Creates constraints defining the size of the receiver. + /// + /// - Parameters: + /// - width: The constant width value. + /// - height: The constant height value. + /// + /// - Returns: The created constraints. + public func sizeConstraints( + width: CGFloat, + height: CGFloat + ) -> [NSLayoutConstraint] { + sizeConstraints(CGSize(width: width, height: height)) + } + // MARK: - Width /// Creates a constraint defining the width of the receiver. diff --git a/Tests/LayoutTests/LayoutItemTests.swift b/Tests/LayoutTests/LayoutItemTests.swift index 75ee6773..5d862eea 100644 --- a/Tests/LayoutTests/LayoutItemTests.swift +++ b/Tests/LayoutTests/LayoutItemTests.swift @@ -61,45 +61,45 @@ final class LayoutItemTests: XCTestCase { // MARK: - Size - func testSizeWidthHeightPriority() { + func testSizePriority() { assertLayout { view in view.layout { - // Size Width and Height with Default Priority + // Size CGSize with Default Priority pinkView .to([.top, .leading]) - .size(width: 150, height: 250, priority: .high) - .size(width: 100, height: 200) + .size(CGSize(width: 150, height: 250), priority: .high) + .size(CGSize(width: 100, height: 200)) - // Size Width and Height with Priority + // Size CGSize with Priority yellowView .to([.top, .trailing]) - .size(width: 50, height: 50, priority: .low) - .size(width: 100, height: 200, priority: .high) + .size(CGSize(width: 50, height: 50), priority: .low) + .size(CGSize(width: 100, height: 200), priority: .high) } .activate() } } - func testSizeWithSizePriority() { + func testSizeWidthHeightPriority() { assertLayout { view in view.layout { - // Size CGSize with Default Priority + // Size Width and Height with Default Priority pinkView .to([.top, .leading]) - .size(CGSize(width: 150, height: 250), priority: .high) - .size(CGSize(width: 100, height: 200)) + .size(width: 150, height: 250, priority: .high) + .size(width: 100, height: 200) - // Size CGSize with Priority + // Size Width and Height with Priority yellowView .to([.top, .trailing]) - .size(CGSize(width: 50, height: 50), priority: .low) - .size(CGSize(width: 100, height: 200), priority: .high) + .size(width: 50, height: 50, priority: .low) + .size(width: 100, height: 200, priority: .high) } .activate() } diff --git a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift index 7878f032..9b50a7fb 100644 --- a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift +++ b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift @@ -259,6 +259,57 @@ final class UIViewAutoLayoutTests: XCTestCase { constant: 40))) } + func testSizeConstraintsWithWidthAndHeight() { + + // GIVEN + + let view: UIView = .init() + + // WHEN + + let sizeConstraints1: [NSLayoutConstraint] = view.sizeConstraints(width: 10, height: 20) + + // THEN + + expect(sizeConstraints1.count) == 2 + expect(sizeConstraints1[0]).to(match(NSLayoutConstraint(item: view, + attribute: .width, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 10))) + expect(sizeConstraints1[1]).to(match(NSLayoutConstraint(item: view, + attribute: .height, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 20))) + + // WHEN + + let sizeConstraints2: [NSLayoutConstraint] = view.sizeConstraints(CGSize(width: 20, height: 40)) + + // THEN + + expect(sizeConstraints2.count) == 2 + expect(sizeConstraints2[0]).to(match(NSLayoutConstraint(item: view, + attribute: .width, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 20))) + expect(sizeConstraints2[1]).to(match(NSLayoutConstraint(item: view, + attribute: .height, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: 40))) + } + // MARK: - Width func testWidthConstraintIsRelationToConstant() { diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-mini-portrait.png b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-mini-portrait.png similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-mini-portrait.png rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-mini-portrait.png diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-mini-portrait.txt b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-mini-portrait.txt similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-mini-portrait.txt rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-mini-portrait.txt diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-portrait.png b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-portrait.png similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-portrait.png rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-portrait.png diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-portrait.txt b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-portrait.txt similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-13-portrait.txt rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-13-portrait.txt diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-8-portrait.png b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-8-portrait.png similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-8-portrait.png rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-8-portrait.png diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-8-portrait.txt b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-8-portrait.txt similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-8-portrait.txt rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-8-portrait.txt diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-SE-portrait.png b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-SE-portrait.png similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-SE-portrait.png rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-SE-portrait.png diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-SE-portrait.txt b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-SE-portrait.txt similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-SE-portrait.txt rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-SE-portrait.txt diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-X-portrait.png b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-X-portrait.png similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-X-portrait.png rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-X-portrait.png diff --git a/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-X-portrait.txt b/Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-X-portrait.txt similarity index 100% rename from Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizeWithSizePriority.iPhone-X-portrait.txt rename to Tests/LayoutTests/__Snapshots__/LayoutItemTests/testSizePriority.iPhone-X-portrait.txt diff --git a/cheatsheet.html b/cheatsheet.html index 0305461a..46c4cc13 100644 --- a/cheatsheet.html +++ b/cheatsheet.html @@ -273,6 +273,8 @@
sizeConstraints()
sizeConstraints(size)+
sizeConstraints(width: width, + height: height)
widthConstraint(is: relation)
widthConstraint(is: relation,