Created
March 26, 2018 19:01
-
-
Save chrisbrandow/b3aa5c03c506873c0983d57485eb8215 to your computer and use it in GitHub Desktop.
Eidhof's Autolayout DSL with a couple additions
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
typealias Constraint = (_ child: UIView, _ parent: UIView) -> NSLayoutConstraint | |
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, _ to: KeyPath<UIView, Anchor>, _ constant: CGFloat = 0) -> Constraint where Anchor: NSLayoutAnchor<Axis> { | |
return { $0[keyPath: keyPath].constraint(equalTo: $1[keyPath: to], constant: constant) } | |
} | |
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, _ constant: CGFloat = 0) -> Constraint where Anchor: NSLayoutAnchor<Axis> { | |
return equal(keyPath, keyPath, constant) | |
} | |
func ==<Axis, Anchor>(_ left: KeyPath<UIView, Anchor>, _ right: KeyPath<UIView, Anchor>) -> Constraint where Anchor: NSLayoutAnchor<Axis> { | |
return equal(left, right) | |
} | |
//////////////////// | |
extension UIView { | |
func addSubview(_ child: UIView, constraints: [Constraint]) { | |
addSubview(child) | |
child.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate(constraints.map { $0(child, self) }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment