In SwiftUI you are able to pass multiple views as one parameter to another view, thanks to @ViewBuilder
. Each child view is treated individually, and so after being passed, can be contained in a VStack
, HStack
etc.
My question is: is it possible for this container view to individually modify each child view passed to it? For example, by adding a Spacer
between each child view.
Here is the equivalent functionality I'm looking for:
struct SomeContainerView<Content: View> {
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
let content: Content
var body: some View {
HStack {
ForEach(content.childViews, id: \.id) { child in
// Give each child view a background
// Add a Spacer in between each child view
// Conditional formatting
// Etc.
}
}
}
}
I believe this to be possible as standard SwiftUI container views can influence the appearance and layout of child views passed to them. Unless this is achieved though internal features of SwiftUI?
A way around this may be to pass in multiple view parameters instead of one, though this is inflexible and messy if many views need to be passed. An array of views doesn't seem to make sense either, as I have not noticed this pattern anywhere in SwiftUI.