Skip to content

Commit

Permalink
Add support for collections and layout guides
Browse files Browse the repository at this point in the history
  • Loading branch information
PimCoumans committed Nov 7, 2022
1 parent 319a903 commit f7133e6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
24 changes: 13 additions & 11 deletions Sources/ConstraintBuilder/ConstraintBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ public extension NSLayoutConstraint {
}
}

public protocol ContraintBuildable {
public protocol ConstraintBuildable {
associatedtype Constrained
/// Create and activate constraints with this view as the main subject
/// ```swift
/// view.applyConstraints {
/// $0.leadingAnchor.constraint(equalTo: otherView.leadingAnchor)
/// $0.centerYAnchor.constraint(equalTo: otherView.centerYAnchor)
/// }
/// ```
/// - Parameter builder: Constraint builder to add the constraints from
func applyConstraints(@ConstraintBuilder _ builder: (Constrained) -> [NSLayoutConstraint])
/// Extends all edges to the edges of the provided view
/// - Parameter view: View of which edges should be extended to
func extend(to view: Self)
func extend(to other: Constrained)

/// Aligns center with center of provided view
/// - Parameter view: View of which center should be aligned to
func center(in view: Self)

/// Extends all edges to the edges of the superview
/// Should result in `assertionFailure` when no superview is available
func extendToSuperview()

/// Aligns center with center of superview
/// Should result in `assertionFailure` when no superview is available
func centerInSuperview()
func center(in other: Constrained)
}
26 changes: 26 additions & 0 deletions Sources/ConstraintBuilder/UILayoutGuide+ConstraintBuildable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if canImport(UIKit)
import UIKit

extension UILayoutGuide: ConstraintBuildable {
public func applyConstraints(@ConstraintBuilder _ builder: (UILayoutGuide) -> [NSLayoutConstraint]) {
NSLayoutConstraint.activate(builder(self))
}

public func extend(to other: UILayoutGuide) {
applyConstraints { _ in
leadingAnchor.constraint(equalTo: other.leadingAnchor)
trailingAnchor.constraint(equalTo: other.trailingAnchor)
topAnchor.constraint(equalTo: other.topAnchor)
bottomAnchor.constraint(equalTo: other.bottomAnchor)
}
}

public func center(in other: UILayoutGuide) {
applyConstraints { _ in
centerXAnchor.constraint(equalTo: other.centerXAnchor)
centerYAnchor.constraint(equalTo: other.centerYAnchor)
}
}
}

#endif
40 changes: 22 additions & 18 deletions Sources/ConstraintBuilder/UIView+ConstraintBuildable.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#if canImport(UIKit)
import UIKit

public protocol UIViewConstraintBuildable: ContraintBuildable {
/// Create and activate constriants with this view as the main subject
/// ```swift
/// view.applyConstraints {
/// $0.leadingAnchor.constraint(equalTo: otherView.leadingAnchor)
/// $0.centerYAnchor.constraint(equalTo: otherView.centerYAnchor)
/// }
/// ```
/// - Parameter builder: Constraint builder to add the constriants from
func applyConstraints(@ConstraintBuilder _ builder: (UIView) -> [NSLayoutConstraint])
extension Collection where Element: ConstraintBuildable {
public func applyConstraints(@ConstraintBuilder _ builder: (Element) -> [NSLayoutConstraint]) {
NSLayoutConstraint.activate(flatMap { builder($0) })
}
}

public protocol UIViewConstraintBuildable: ConstraintBuildable {
/// Extends all edges to the edges of the superview
/// Should result in `assertionFailure` when no superview is available
func extendToSuperview()

/// Aligns center with center of superview
/// Should result in `assertionFailure` when no superview is available
func centerInSuperview()

/// Extends all edges to provided layout guide
/// - Parameter view: UILayoutGuide of which edges should be extended to
Expand All @@ -35,19 +39,19 @@ extension UIView: UIViewConstraintBuildable {
NSLayoutConstraint.activate(builder(self))
}

public func extend(to view: UIView) {
public func extend(to other: UIView) {
applyConstraints { _ in
leadingAnchor.constraint(equalTo: view.leadingAnchor)
trailingAnchor.constraint(equalTo: view.trailingAnchor)
topAnchor.constraint(equalTo: view.topAnchor)
bottomAnchor.constraint(equalTo: view.bottomAnchor)
leadingAnchor.constraint(equalTo: other.leadingAnchor)
trailingAnchor.constraint(equalTo: other.trailingAnchor)
topAnchor.constraint(equalTo: other.topAnchor)
bottomAnchor.constraint(equalTo: other.bottomAnchor)
}
}

public func center(in view: UIView) {
public func center(in other: UIView) {
applyConstraints { _ in
centerXAnchor.constraint(equalTo: view.centerXAnchor)
centerYAnchor.constraint(equalTo: view.centerYAnchor)
centerXAnchor.constraint(equalTo: other.centerXAnchor)
centerYAnchor.constraint(equalTo: other.centerYAnchor)
}
}

Expand Down

0 comments on commit f7133e6

Please sign in to comment.