Skip to content

Commit 2d65068

Browse files
devkaranCTmpospese
authored andcommitted
CM-1012: "constrainAspectRatio" method defined
1 parent faeb833 commit 2d65068

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Sources/YCoreUI/Extensions/UIKit/UIView+constrainAspectRatio.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,31 @@
77
//
88

99
import UIKit
10+
11+
extension UIView {
12+
/// constrain the aspect ratio for the receiving view
13+
/// - Parameters:
14+
/// - ratio: aspect ratio
15+
/// - offset: offset to apply (default `.zero`)
16+
/// - relation: relation to evaluate (towards dimension) (default `.equal`)
17+
/// - priority: constraint priority (default `.required`)
18+
/// - isActive: whether to activate the constraint or not (default `true`)
19+
/// - Returns: The created layout constraint
20+
@discardableResult public func constrainAspectRatio(
21+
_ ratio: CGFloat,
22+
offset: CGFloat = 0,
23+
relatedBy relation: NSLayoutConstraint.Relation = .equal,
24+
priority: UILayoutPriority = .required,
25+
isActive: Bool = true
26+
) -> NSLayoutConstraint {
27+
constrain(
28+
.widthAnchor,
29+
to : heightAnchor,
30+
relatedBy : relation,
31+
multiplier: ratio,
32+
constant : offset,
33+
priority: priority,
34+
isActive: isActive
35+
)
36+
}
37+
}

0 commit comments

Comments
 (0)