Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add square constraints method #338

Merged
merged 1 commit into from
Feb 15, 2024
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
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
Loading