Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Sources/Layout/LayoutItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 14 additions & 14 deletions Tests/LayoutTests/LayoutItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
51 changes: 51 additions & 0 deletions Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ <h3>Builder</h3>
<h3>Size</h3>
<pre>sizeConstraints()</pre>
<pre>sizeConstraints(size)</pre>
<pre>sizeConstraints(width: width,
height: height)</pre>
<h3>Width</h3>
<pre>widthConstraint(is: relation)</pre>
<pre>widthConstraint(is: relation,
Expand Down