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 aspectRatio constraint helper #254

Merged
merged 2 commits into from
Jul 26, 2022
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
4 changes: 2 additions & 2 deletions Alicerce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,7 @@
48BF456F23D1FD0C00DFBB28 /* CenterConstrainableProxyTestCase.swift */,
48BF456623D1D26F00DFBB28 /* CenterXConstrainableProxyTestCase.swift */,
48BF456923D1D4A300DFBB28 /* CenterYConstrainableProxyTestCase.swift */,
3E8D61932546F8AF00C08EA2 /* ConstraintGroupToggleTestCase.swift */,
4838FE4323A950CB007311F0 /* EdgesConstrainableProxyTestCase.swift */,
48A5ECC623D5AC3A0014B2B7 /* FirstBaselineConstrainableProxyTestCase.swift */,
48BF456323D1CFB300DFBB28 /* HeightConstrainableProxyTestCase.swift */,
Expand All @@ -1481,12 +1482,11 @@
4838FE3E23A950CA007311F0 /* NSLayoutConstraintTestCase.swift */,
4838FE3A23A950CA007311F0 /* RightConstrainableProxyTestCase.swift */,
48BF457223D204DC00DFBB28 /* SizeConstrainableProxyTestCase.swift */,
48A5ECCF23D5B9F70014B2B7 /* TopBottomConstrainableProxyTestCase.swift */,
4838FE4023A950CA007311F0 /* TopConstrainableProxyTestCase.swift */,
4838FE3B23A950CA007311F0 /* TrailingConstrainableProxyTestCase.swift */,
4833D5B223D0D28C00EBD925 /* WidthConstrainableProxyTestCase.swift */,
4838FE3C23A950CA007311F0 /* XCTAssertConstraint.swift */,
48A5ECCF23D5B9F70014B2B7 /* TopBottomConstrainableProxyTestCase.swift */,
3E8D61932546F8AF00C08EA2 /* ConstraintGroupToggleTestCase.swift */,
);
path = AutoLayout;
sourceTree = "<group>";
Expand Down
18 changes: 18 additions & 0 deletions Sources/AutoLayout/ConstrainableProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,24 @@ public extension SizeConstrainableProxy {

return constraints
}

@discardableResult
func aspectRatio(
_ multiplier: CGFloat = 1,
offset: CGFloat = 0,
relation: ConstraintRelation = .equal,
priority: UILayoutPriority = .required
) -> NSLayoutConstraint {

constrain(
from: width,
to: height,
multiplier: multiplier,
constant: offset,
relation: relation,
priority: priority
)
}
}

public protocol BaselineConstrainableProxy: ConstrainableProxy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,63 @@ class SizeConstrainableProxyTestCase: BaseConstrainableProxyTestCase {
XCTAssert(constraintGroup1.isActive)
XCTAssertEqual(view0.frame.size, Constants.size1)
}

func testConstrain_WithAbsoluteWidthAndAspectRatioConstraint_ShouldSupportRelativeSize() {

var constraint: NSLayoutConstraint!

constrain(view0) { view0 in
view0.width(1920)
constraint = view0.aspectRatio(16/9)
}

let expected = NSLayoutConstraint(
item: view0!,
attribute: .width,
relatedBy: .equal,
toItem: view0!,
attribute: .height,
multiplier: 16/9,
constant: 0,
priority: .required,
active: true
)

XCTAssertConstraint(expected, constraint)

host.layoutIfNeeded()

XCTAssertEqual(view0.frame.size, CGSize(width: 1920, height: 1080))
}

func testConstrain_WithRelativeHeightAndAspectRatioConstraint_ShouldSupportRelativeSize() {

var constraint: NSLayoutConstraint!

constrain(view0, host) { view0, host in
view0.top(to: host, offset: 100)
view0.bottom(to: host)
constraint = view0.aspectRatio(0.5)
}

let expected = NSLayoutConstraint(
item: view0!,
attribute: .width,
relatedBy: .equal,
toItem: view0!,
attribute: .height,
multiplier: 0.5,
constant: 0,
priority: .required,
active: true
)

XCTAssertConstraint(constraint, expected)

host.layoutIfNeeded()

XCTAssertEqual(view0.frame.size, CGSize(width: 200, height: 400))
}
}

private extension SizeConstrainableProxyTestCase {
Expand Down