ViewBuilder
A custom parameter attribute that constructs views from closures.
It allows to build reusable views by separating its presentation logic and content.
It has declarations of buildBlock method, which takes from one to max ten child views, and all of them combine child views into a TupleView. That’s why ViewBuilder can accept only ten views inside the closure.
ViewBuilder with 2 views
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension ViewBuilder {
public static func buildBlock<C0, C1>(_ c0: C0, _ c1: C1) -> TupleView<(C0, C1)> where C0 : View, C1 : View
}
ViewBuilder with 10 views
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension ViewBuilder {static func buildBlock<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)>
}
You can also use ViewBuilders to create your custom container views, like HStack , VStack.
If you look inside the VStack or HStack, you’ll find they use ViewBuilder to construct views from closures.
Let’s try creating a custom container view using ViewBuilders
Reference