Skip to content

Instantly share code, notes, and snippets.

@floriankugler
Last active August 30, 2023 21:17
Show Gist options
  • Save floriankugler/7cbc30b006ddb0ee0099997957604890 to your computer and use it in GitHub Desktop.
Save floriankugler/7cbc30b006ddb0ee0099997957604890 to your computer and use it in GitHub Desktop.
Very simple key path based Auto Layout helpers
import UIKit
typealias Constraint = (UIView, UIView) -> NSLayoutConstraint
func equal<L, Axis>(_ to: KeyPath<UIView, L>, constant: CGFloat = 0) -> Constraint where L: NSLayoutAnchor<Axis> {
return equal(to, to, constant: constant)
}
func equal<L, Axis>(_ from: KeyPath<UIView, L>, _ to: KeyPath<UIView, L>, constant: CGFloat = 0) -> Constraint where L: NSLayoutAnchor<Axis> {
return { view1, view2 in
view1[keyPath: from].constraint(equalTo: view2[keyPath: to], constant: constant)
}
}
func equal<L>(_ keyPath: KeyPath<UIView, L>, constant: CGFloat) -> Constraint where L: NSLayoutDimension {
return { view1, _ in
view1[keyPath: keyPath].constraint(equalToConstant: constant)
}
}
extension UIView {
func addSubview(_ other: UIView, constraints: [Constraint]) {
other.translatesAutoresizingMaskIntoConstraints = false
addSubview(other)
addConstraints(constraints.map { $0(other, self) })
}
}
let v1 = UIView()
v1.backgroundColor = .red
v1.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
let v2 = UIView()
v2.backgroundColor = .green
v1.addSubview(v2, constraints:[
equal(\.centerYAnchor, \.bottomAnchor),
equal(\.leftAnchor, constant: 10), equal(\.rightAnchor, constant: -10),
equal(\.heightAnchor, constant: 100)
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment