Skip to content

Instantly share code, notes, and snippets.

@boek
Last active June 28, 2022 17:46
Show Gist options
  • Save boek/2d90898916e2b97662aa to your computer and use it in GitHub Desktop.
Save boek/2d90898916e2b97662aa to your computer and use it in GitHub Desktop.
extension UIView {
static func build<T: UIView>(builder: ((T) -> ())? = nil) -> T {
let view = T(frame: .zero)
view.setTranslatesAutoresizingMaskIntoConstraints(false)
builder?(view)
return view
}
}
// Usage
let toggleWrapper: UIView = .build { view in
view.backgroundColor = UIColor.n7Color()
}
lazy var customLabel: UILabel = .build { label in
label.textColor = UIColor.redColor()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment