Skip to content

Commit

Permalink
Merge pull request #3 from PimCoumans/feature/improved-generics
Browse files Browse the repository at this point in the history
Improved generics
  • Loading branch information
PimCoumans authored Mar 8, 2023
2 parents 6a9cdfd + 577a0ab commit 37126ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/ConstraintBuilder/ConstraintBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extension LayoutGuide: ConstraintBuildable {
/// Convenience methods to apply layout constraints to views
public protocol ViewConstraintBuildable: ConstraintBuildable where Constrained: LayoutContainerView {
/// Use superview if available, `assertionFailure()` if not
func withSuperview(_ method: (Constrained) -> Void)
func withSuperview(_ method: (LayoutContainerView) -> Void)

/// Extends all edges to the edges of the superview
/// Should result in `assertionFailure` when no superview is available
Expand Down
7 changes: 4 additions & 3 deletions Sources/ConstraintBuilder/NSView+ConstraintBuildable.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#if canImport(AppKit)
import AppKit

extension NSView: ViewConstraintBuildable {
public func withSuperview(_ method: (NSView) -> Void) {
extension ViewConstraintBuildable where Self: NSView {
public func withSuperview(_ method: (LayoutContainerView) -> Void) {
guard let superview else {
return assertionFailure()
}
method(superview)
}

public func applyConstraints(@ConstraintBuilder _ builder: (NSView) -> [NSLayoutConstraint]) {
public func applyConstraints(@ConstraintBuilder _ builder: (Self) -> [NSLayoutConstraint]) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(builder(self))
}
}
extension NSView: ViewConstraintBuildable { }
#endif
7 changes: 4 additions & 3 deletions Sources/ConstraintBuilder/UIView+ConstraintBuildable.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#if canImport(UIKit)
import UIKit

extension UIView: ViewConstraintBuildable {
public func withSuperview(_ method: (UIView) -> Void) {
extension ViewConstraintBuildable where Self: UIView {
public func withSuperview(_ method: (LayoutContainerView) -> Void) {
guard let superview else {
return assertionFailure()
}
method(superview)
}

public func applyConstraints(@ConstraintBuilder _ builder: (UIView) -> [NSLayoutConstraint]) {
public func applyConstraints(@ConstraintBuilder _ builder: (Self) -> [NSLayoutConstraint]) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(builder(self))
}
}
extension UIView: ViewConstraintBuildable { }
#endif
24 changes: 24 additions & 0 deletions Tests/ConstraintBuilderTests/ConstraintBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import XCTest
import ConstraintBuilder

final class ConstraintBuilderTests: XCTestCase {
class CustomView: UIView {
let customGuide = UILayoutGuide()
override init(frame: CGRect) {
super.init(frame: frame)
addLayoutGuide(customGuide)
customGuide.extend(to: self)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
#if canImport(UIKit) || canImport(tvOS)
var superview: UIView!
var view: UIView!
Expand Down Expand Up @@ -57,4 +69,16 @@ final class ConstraintBuilderTests: XCTestCase {
func testViewExtendToGuide() {
view.extend(to: guide)
}

func testCustomViewConstraints() {
let customView = CustomView()
superview.addSubview(customView)
customView.extendToSuperviewLayoutMargins()
customView.applyConstraints {
$0.customGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor)
$0.customGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor)
$0.customGuide.topAnchor.constraint(equalTo: view.topAnchor)
$0.customGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor)
}
}
}

0 comments on commit 37126ae

Please sign in to comment.