-
Notifications
You must be signed in to change notification settings - Fork 7
Added extension for UIView constrain center #21
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
dharmik-yml
merged 4 commits into
codeandtheory:main
from
dharmik-yml:bugfix-CM-868-constrainCenter
Oct 21, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a0d088d
Added extension for UIView constrain center
dharmik-yml 98eec34
Improved documentation comment and fixed control
dharmik-yml ceed40b
Further improved the documentation comments
dharmik-yml 6b20a21
Added line above static elements
dharmik-yml 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
75 changes: 75 additions & 0 deletions
75
Sources/YCoreUI/Extensions/UIKit/UIView+constrainCenter.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,75 @@ | ||
// | ||
// UIView+constrainCenter.swift | ||
// YCoreUI | ||
// | ||
// Created by Dharmik Ghelani on 20/10/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
/// Center alignment options | ||
public struct Center: OptionSet { | ||
/// corresponding raw value | ||
public let rawValue: UInt | ||
|
||
/// center X | ||
public static let x = Center(rawValue: 1 << 0) | ||
/// center Y | ||
public static let y = Center(rawValue: 1 << 1) | ||
/// all (both center X and center Y) | ||
public static let all: Center = [.x, .y] | ||
|
||
/// Creates the new `Center` option set from the given raw value. | ||
/// - Parameter rawValue: the raw value of `Center` option set to create | ||
public init(rawValue: UInt) { | ||
self.rawValue = rawValue | ||
} | ||
} | ||
|
||
/// Constrain the center of the receiving view with the center of another view | ||
/// - Parameters: | ||
/// - center: which center attributes to constrain (default `.all`) | ||
/// - view2: view or layout guide to constrain to (pass `nil` to constrain to superview) | ||
/// - relation: relation to evaluate (towards view2) (default `.equal`) | ||
/// - offset: offset to apply relative to view2.center (default `.zero`) | ||
/// - priority: constraint priority (default `.required`) | ||
/// - isActive: whether to activate the constraints or not (default `true`) | ||
/// - Returns: dictionary of constraints created, keyed by `.centerX, .centerY` | ||
public func constrainCenter( | ||
_ center: Center = .all, | ||
to view2: Anchorable? = nil, | ||
relatedBy relation: NSLayoutConstraint.Relation = .equal, | ||
offset: UIOffset = .zero, | ||
priority: UILayoutPriority = .required, | ||
isActive: Bool = true | ||
) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint] { | ||
var constraints: [NSLayoutConstraint.Attribute: NSLayoutConstraint] = [:] | ||
let otherView: Anchorable! = view2 ?? superview | ||
|
||
if center.contains(.x) { | ||
constraints[.centerX] = constrain( | ||
.centerXAnchor, | ||
to: otherView.centerXAnchor, | ||
relatedBy: relation, | ||
constant: offset.horizontal, | ||
priority: priority, | ||
isActive: isActive | ||
) | ||
} | ||
|
||
if center.contains(.y) { | ||
constraints[.centerY] = constrain( | ||
.centerYAnchor, | ||
to: otherView.centerYAnchor, | ||
relatedBy: relation, | ||
constant: offset.vertical, | ||
priority: priority, | ||
isActive: isActive | ||
) | ||
} | ||
|
||
return constraints | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
Tests/YCoreUITests/Extensions/UIKit/UIView+constrainCenterTests.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,84 @@ | ||
// | ||
// UIView+constrainCenterTests.swift | ||
// YCoreUI | ||
// | ||
// Created by Dharmik Ghelani on 20/10/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import YCoreUI | ||
|
||
final class UIViewConstrainCenterTests: XCTestCase { | ||
func testCenterX() { | ||
let (sut, offset) = makeSUT() | ||
|
||
let constraints = sut.view1.constrainCenter(.x, offset: offset) | ||
let centerXConstraint = constraints[.centerX] | ||
|
||
XCTAssertFalse(sut.view1.translatesAutoresizingMaskIntoConstraints) | ||
XCTAssertEqual(constraints.count, 1) | ||
XCTAssertNotNil(centerXConstraint) | ||
XCTAssertEqual(centerXConstraint?.firstItem as? UIView, sut.view1) | ||
XCTAssertEqual(centerXConstraint?.firstAttribute, .centerX) | ||
XCTAssertEqual(centerXConstraint?.secondItem as? UIView, sut) | ||
XCTAssertEqual(centerXConstraint?.secondAttribute, .centerX) | ||
XCTAssertEqual(centerXConstraint?.constant, offset.horizontal) | ||
} | ||
|
||
func testCenterY() { | ||
let (sut, offset) = makeSUT() | ||
|
||
let constraints = sut.view1.constrainCenter(.y, offset: offset) | ||
let centerYConstraint = constraints[.centerY] | ||
|
||
XCTAssertFalse(sut.view1.translatesAutoresizingMaskIntoConstraints) | ||
XCTAssertEqual(constraints.count, 1) | ||
XCTAssertNotNil(centerYConstraint) | ||
XCTAssertEqual(centerYConstraint?.firstItem as? UIView, sut.view1) | ||
XCTAssertEqual(centerYConstraint?.firstAttribute, .centerY) | ||
XCTAssertEqual(centerYConstraint?.secondItem as? UIView, sut) | ||
XCTAssertEqual(centerYConstraint?.secondAttribute, .centerY) | ||
XCTAssertEqual(centerYConstraint?.constant, offset.vertical) | ||
} | ||
|
||
func testCenterBoth() { | ||
let (sut, offset) = makeSUT() | ||
|
||
let constraints = sut.view1.constrainCenter(offset: offset) | ||
|
||
let anchorAttributes: [ | ||
(NSLayoutConstraint.Attribute, CGFloat) | ||
] = [ | ||
(.centerX, offset.horizontal), | ||
(.centerY, offset.vertical) | ||
] | ||
|
||
XCTAssertFalse(sut.view1.translatesAutoresizingMaskIntoConstraints) | ||
XCTAssertEqual(constraints.count, 2) | ||
|
||
anchorAttributes.forEach { | ||
let constraint = constraints[$0.0] | ||
XCTAssertNotNil(constraint) | ||
XCTAssertEqual(constraint?.firstItem as? UIView, sut.view1) | ||
XCTAssertEqual(constraint?.firstAttribute, $0.0) | ||
XCTAssertEqual(constraint?.secondItem as? UIView, sut) | ||
XCTAssertEqual(constraint?.secondAttribute, $0.0) | ||
XCTAssertEqual(constraint?.constant, $0.1) | ||
} | ||
} | ||
} | ||
|
||
extension UIViewConstrainCenterTests { | ||
func makeSUT( | ||
file: StaticString = #filePath, | ||
line: UInt = #line | ||
) -> (MockLayoutContainer, UIOffset) { | ||
let container = MockLayoutContainer() | ||
let offset = UIOffset(horizontal: 30, vertical: 20) | ||
|
||
trackForMemoryLeak(container, file: file, line: line) | ||
|
||
return (container, offset) | ||
} | ||
} |
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.