Last active
October 23, 2022 00:54
-
-
Save cjnevin/526e70a17e0b229727a4ddf121d6050b to your computer and use it in GitHub Desktop.
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
| @resultBuilder | |
| public enum AutoLayoutBuilder { | |
| public static func buildBlock(_ components: Constrainable...) -> [NSLayoutConstraint] { | |
| components.flatMap(\.constraints) | |
| } | |
| } | |
| extension UIView { | |
| @discardableResult public func addSubview<View: UIView>(_ view: View, @AutoLayoutBuilder with makeConstraints: (View) -> [Constrainable]) -> [NSLayoutConstraint] { | |
| view.translatesAutoresizingMaskIntoConstraints = false | |
| addSubview(view) | |
| let constraints = makeConstraints(view).flatMap(\.constraints) | |
| NSLayoutConstraint.activate(constraints) | |
| return constraints | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment