-
Notifications
You must be signed in to change notification settings - Fork 7
[CM-1012] Add constrainAspectRatio
method to UIView extension.
#38
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3baca83
CM-1012: Created "UIView+constrainAspectRatio"
devkaranCT f5a661e
CM-1012: "constrainAspectRatio" method defined
devkaranCT 07ade0d
CM:1012: Added "UIView+constrainAspectRatioTests.swift"
devkaranCT f4416a9
CM-1012: UT in progress
devkaranCT afa640b
[CM-1012] test the defaultValues,
karthikyml 430f0f3
[CM-1012]
karthikyml 67485e2
CM-1012: Removed white spaces
devkaranCT 03e21e6
CM-1012: Fixed swiftlint violations
devkaranCT 6183cfd
[CM-1012] Updated documentation comment.
devkaranCT b8392a9
[CM-1012]: Removed '@testable'.
devkaranCT bbaa2fa
[CM-1012]: Fixed Typo.
devkaranCT 4885cbf
[CM-1012]: Comments addressed.
devkaranCT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Sources/YCoreUI/Extensions/UIKit/UIView+constrainAspectRatio.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// UIView+constrainAspectRatio.swift | ||
// YCoreUI | ||
// | ||
// Created by Dev Karan on 14/12/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
/// Constrain the aspect ratio for the receiving view. | ||
/// - Parameters: | ||
/// - ratio: aspect ratio | ||
/// - offset: offset to apply (default `.zero`) | ||
/// - relation: relation to evaluate (towards ratio) (default `.equal`) | ||
/// - priority: constraint priority (default `.required`) | ||
/// - isActive: whether to activate the constraint or not (default `true`) | ||
/// - Returns: the created layout constraint | ||
@discardableResult public func constrainAspectRatio( | ||
_ ratio: CGFloat, | ||
offset: CGFloat = 0, | ||
relatedBy relation: NSLayoutConstraint.Relation = .equal, | ||
priority: UILayoutPriority = .required, | ||
isActive: Bool = true | ||
) -> NSLayoutConstraint { | ||
constrain( | ||
.widthAnchor, | ||
to: heightAnchor, | ||
relatedBy: relation, | ||
multiplier: ratio, | ||
constant: offset, | ||
priority: priority, | ||
isActive: isActive | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Tests/YCoreUITests/Extensions/UIKit/UIView+constrainAspectRatioTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// UIView+constrainAspectRatioTests.swift | ||
// YCoreUI | ||
// | ||
// Created by Dev Karan on 14/12/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import YCoreUI | ||
|
||
final class UIViewContrainAspectRatioTests: XCTestCase { | ||
func test_constrainAspectRatio_deliversDefaultValues() { | ||
// Arrange | ||
let sut = makeSUT() | ||
// Act | ||
let constraint = sut.constrainAspectRatio(0.5) | ||
// Assert | ||
XCTAssertEqual(constraint.constant, .zero) | ||
XCTAssertEqual(constraint.priority, .required) | ||
XCTAssertTrue(constraint.isActive) | ||
XCTAssertEqual(constraint.relation, .equal) | ||
XCTAssertEqual(constraint.firstAttribute, .width) | ||
XCTAssertEqual(constraint.secondAttribute, .height) | ||
} | ||
|
||
func test_constrainAspectRatio_translatesAutoresizingMaskIntoConstraintsIsFalse() { | ||
// Arrange | ||
let sut = makeSUT() | ||
// Act | ||
sut.constrainAspectRatio(0.5) | ||
// Assert | ||
XCTAssertFalse(sut.translatesAutoresizingMaskIntoConstraints) | ||
} | ||
|
||
func test_constrainAspectRatio_multiplierAndRatioMatches() { | ||
// Arrange | ||
let sut = makeSUT() | ||
let ratio = 0.5 | ||
// Act | ||
let constraint = sut.constrainAspectRatio(ratio) | ||
// Assert | ||
XCTAssertEqual(constraint.multiplier, ratio) | ||
} | ||
|
||
func test_constrainAspectRatio_resizesSUTWithGivenRatio() { | ||
// Arrange | ||
let containerView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 500, height: 500))) | ||
let sut = makeSUT() | ||
containerView.addSubview(sut) | ||
|
||
let ratio: CGFloat = 0.7 | ||
let height: CGFloat = 300 | ||
sut.constrain(.heightAnchor, constant: height) | ||
// Act | ||
sut.constrainAspectRatio(ratio) | ||
sut.layoutIfNeeded() | ||
// Assert | ||
XCTAssertEqual(sut.bounds.width, ratio * sut.bounds.height) | ||
XCTAssertEqual(sut.bounds.height, height) | ||
} | ||
} | ||
|
||
private extension UIViewContrainAspectRatioTests { | ||
func makeSUT( | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) -> UIView { | ||
let sut = UIView() | ||
trackForMemoryLeak(sut, file: file, line: line) | ||
return sut | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.