diff --git a/Sources/Layout/UIKit/UIView+AutoLayout.swift b/Sources/Layout/UIKit/UIView+AutoLayout.swift index 27366a9b..8a1160ce 100644 --- a/Sources/Layout/UIKit/UIView+AutoLayout.swift +++ b/Sources/Layout/UIKit/UIView+AutoLayout.swift @@ -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. diff --git a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift index 7000c37a..13888a2b 100644 --- a/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift +++ b/Tests/LayoutTests/UIKit/UIView+AutoLayoutTests.swift @@ -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 diff --git a/cheatsheet.html b/cheatsheet.html index ea056e34..200dcfad 100644 --- a/cheatsheet.html +++ b/cheatsheet.html @@ -285,6 +285,8 @@

Height

heightConstraint()
heightConstraint(100)

Square

+
// Size //
+
squareConstraints(100)
// Aspect Ratio //
squareConstraint()

Aspect Ratio