Skip to content

Commit

Permalink
Add square constraints method (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller authored Feb 15, 2024
1 parent 5ef2f1f commit 2608a69
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/Layout/UIKit/UIView+AutoLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ extension UIView {

// MARK: - Square

/// Creates constraints defining the width and height of the receiver to be the given length.
///
/// - Returns: The created constraints.
public func squareConstraints(_ length: CGFloat) -> [NSLayoutConstraint] {
[
widthConstraint(length),
heightConstraint(length)
]
}

/// Creates a constraint defining the aspect ratio of the receiver to be square.
///
/// - Returns: The created constraint.
Expand Down
29 changes: 29 additions & 0 deletions Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,35 @@ final class UIViewAutoLayoutTests: XCTestCase {

// MARK: - Square

func testSquareConstraintsWithLength() {

// GIVEN

let view: UIView = .init()

// WHEN

let squareConstraints: [NSLayoutConstraint] = view.squareConstraints(100)

// THEN

expect(squareConstraints.count) == 2
expect(squareConstraints[0]).to(match(NSLayoutConstraint(item: view,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 100)))
expect(squareConstraints[1]).to(match(NSLayoutConstraint(item: view,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: 100)))
}

func testSquareConstraint() {

// GIVEN
Expand Down
2 changes: 2 additions & 0 deletions cheatsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ <h3>Height</h3>
<pre>heightConstraint()</pre>
<pre>heightConstraint(100)</pre>
<h3>Square</h3>
<pre>// Size //</pre>
<pre>squareConstraints(100)</pre>
<pre>// Aspect Ratio //</pre>
<pre>squareConstraint()</pre>
<h3>Aspect Ratio</h3>
Expand Down

0 comments on commit 2608a69

Please sign in to comment.