@ViewBuilder is a result builder that lets you write multiple views inside a closure and combine them into a single some View.
SwiftUI requires a single return type (some View), but we often write multiple views.
@ViewBuilder transforms:
VStack {
Text("A")
Text("B")
}
into a single combined view.
VStack
HStack
ZStack
Group
custom view builders
VStack {
Text("A")
if condition {
Text("B")
} else {
Text("C")
}
}
for item in items { } β
while condition { } β
Use:
ForEach(...) β