-
Notifications
You must be signed in to change notification settings - Fork 7
Added constrainSize extension #17
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
mpospese
merged 4 commits into
codeandtheory:main
from
dharmik-yml:bugfix-CM-867-constrainSize
Oct 20, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
77 changes: 77 additions & 0 deletions
77
Sources/YCoreUI/Extensions/UIKit/UIView+constrainSize.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,77 @@ | ||
// | ||
// UIView+constrainSize.swift | ||
// YCoreUI | ||
// | ||
// Created by Dharmik Ghelani on 17/10/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
/// Constrain the receiving view with given size | ||
/// - Parameters: | ||
/// - size: size of view to constrain to | ||
/// - relation: relation to evaluate (towards size) (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 constrainSize( | ||
_ size: CGSize, | ||
relatedBy relation: NSLayoutConstraint.Relation = .equal, | ||
priority: UILayoutPriority = .required, | ||
isActive: Bool = true | ||
) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint] { | ||
let widthConstraint = constrain( | ||
.widthAnchor, | ||
relatedBy: relation, | ||
constant: size.width, | ||
priority: priority, | ||
isActive: isActive | ||
) | ||
|
||
let heightConstraint = constrain( | ||
.heightAnchor, | ||
relatedBy: relation, | ||
constant: size.height, | ||
priority: priority, | ||
isActive: isActive | ||
) | ||
|
||
return [.width: widthConstraint, .height: heightConstraint] | ||
} | ||
|
||
/// Constrain the receiving view with width and heigh | ||
/// - Parameters: | ||
/// - width: width of view to constrain to | ||
/// - height: height of view to constrain to | ||
/// - relation: relation to evaluate (towards width and height) (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 constrainSize( | ||
width: CGFloat, | ||
height: CGFloat, | ||
relatedBy relation: NSLayoutConstraint.Relation = .equal, | ||
priority: UILayoutPriority = .required, | ||
isActive: Bool = true | ||
) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint] { | ||
constrainSize(CGSize(width: width, height: height)) | ||
} | ||
|
||
/// Constrain the receiving view with given dimension | ||
/// - Parameters: | ||
/// - dimension: dimension of view to constrain to | ||
/// - relation: relation to evaluate (towards dimension) (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 constrainSize( | ||
_ dimension: CGFloat, | ||
relatedBy relation: NSLayoutConstraint.Relation = .equal, | ||
priority: UILayoutPriority = .required, | ||
isActive: Bool = true | ||
) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint] { | ||
constrainSize(CGSize(width: dimension, height: dimension)) | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
Tests/YCoreUITests/Extensions/UIKit/UIView+constrainSizeTests.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,125 @@ | ||
// | ||
// UIView+constrainSizeTests.swift | ||
// YCoreUITests | ||
// | ||
// Created by Dharmik Ghelani on 17/10/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import YCoreUI | ||
|
||
final class UIViewConstrainSizeTests: XCTestCase { | ||
let randomSize = CGSize(width: 150, height: 200) | ||
|
||
func testSize() { | ||
let sut = makeSUT() | ||
|
||
let sizeAttributes: [ | ||
(NSLayoutConstraint.Attribute, CGFloat) | ||
] = [ | ||
(.width, randomSize.width), | ||
(.height, randomSize.height) | ||
] | ||
|
||
let constraints = sut.constrainSize(randomSize) | ||
|
||
XCTAssertFalse(sut.translatesAutoresizingMaskIntoConstraints) | ||
|
||
sizeAttributes.forEach { | ||
guard let constraint = constraints[$0.0] else { | ||
XCTFail("Expected to have constraint for give attribute") | ||
return | ||
} | ||
|
||
XCTAssertEqual(constraint.firstItem as? UIView, sut) | ||
XCTAssertEqual(constraint.firstAttribute, $0.0) | ||
XCTAssertNil(constraint.secondItem) | ||
XCTAssertEqual(constraint.secondAttribute, .notAnAttribute) | ||
XCTAssertEqual(constraint.multiplier, 1) | ||
XCTAssertEqual(constraint.constant, $0.1) | ||
XCTAssertEqual(constraint.priority, .required) | ||
XCTAssert(constraint.isActive) | ||
} | ||
} | ||
|
||
func testWidthAndHeight() { | ||
let sut = makeSUT() | ||
|
||
XCTAssert(sut.translatesAutoresizingMaskIntoConstraints) | ||
|
||
let sizeAttributes: [ | ||
(NSLayoutConstraint.Attribute, CGFloat) | ||
] = [ | ||
(.width, randomSize.width), | ||
(.height, randomSize.height) | ||
] | ||
|
||
let constraints = sut.constrainSize(width: randomSize.width, height: randomSize.height) | ||
|
||
XCTAssertFalse(sut.translatesAutoresizingMaskIntoConstraints) | ||
|
||
sizeAttributes.forEach { | ||
guard let constraint = constraints[$0.0] else { | ||
XCTFail("Expected to have constraint for give attribute") | ||
return | ||
} | ||
|
||
XCTAssertEqual(constraint.firstItem as? UIView, sut) | ||
XCTAssertEqual(constraint.firstAttribute, $0.0) | ||
XCTAssertNil(constraint.secondItem) | ||
XCTAssertEqual(constraint.secondAttribute, .notAnAttribute) | ||
XCTAssertEqual(constraint.multiplier, 1) | ||
XCTAssertEqual(constraint.constant, $0.1) | ||
XCTAssertEqual(constraint.priority, .required) | ||
XCTAssert(constraint.isActive) | ||
} | ||
} | ||
|
||
func testDimension() { | ||
let sut = makeSUT() | ||
|
||
XCTAssert(sut.translatesAutoresizingMaskIntoConstraints) | ||
|
||
let randomDimension = 100.0 | ||
|
||
let sizeAttributes: [ | ||
(NSLayoutConstraint.Attribute, CGFloat) | ||
] = [ | ||
(.width, randomDimension), | ||
(.height, randomDimension) | ||
] | ||
|
||
let constraints = sut.constrainSize(randomDimension) | ||
|
||
XCTAssertFalse(sut.translatesAutoresizingMaskIntoConstraints) | ||
|
||
sizeAttributes.forEach { | ||
guard let constraint = constraints[$0.0] else { | ||
XCTFail("Expected to have constraint for give attribute") | ||
return | ||
} | ||
|
||
XCTAssertEqual(constraint.firstItem as? UIView, sut) | ||
XCTAssertEqual(constraint.firstAttribute, $0.0) | ||
XCTAssertNil(constraint.secondItem) | ||
XCTAssertEqual(constraint.secondAttribute, .notAnAttribute) | ||
XCTAssertEqual(constraint.multiplier, 1) | ||
XCTAssertEqual(constraint.constant, $0.1) | ||
XCTAssertEqual(constraint.priority, .required) | ||
XCTAssert(constraint.isActive) | ||
} | ||
} | ||
} | ||
|
||
extension UIViewConstrainSizeTests { | ||
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.