Skip to content

Instantly share code, notes, and snippets.

@Otbivnoe
Created November 25, 2020 10:32
Show Gist options
  • Save Otbivnoe/6cc3c114ec99aef62349d1a3617b001f to your computer and use it in GitHub Desktop.
Save Otbivnoe/6cc3c114ec99aef62349d1a3617b001f to your computer and use it in GitHub Desktop.
Subviews builder
@_functionBuilder
struct SubviewsBuilder {
static func buildBlock(_ views: UIView...) -> [UIView] {
return views
}
}
extension UIView {
func addSubviews(@SubviewsBuilder _ subviews: () -> [UIView]) -> UIView {
subviews().forEach { addSubview($0) }
return self
}
}
let view = UIView()
let containerView = UIView()
view.addSubviews {
containerView.addSubviews {
UIView()
UIView()
}
UIView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment