📦ViewBuilder
ViewBuilder as a Parameter
Using ViewBuilder ⭐️
// ⭐️ Step 1: Create a View struct like this:
// ⭐️ Note : `Content` conforms to `View`
struct MyContainerView<Content: View>: View {
...
}// ⭐️ Step 2: Add `content` property and initializer
struct MyContainerView<Content: View>: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
}// ⭐️ Step 3: Implementing `View` protocol requirement
struct MyContainerView<Content: View>: View {
let content: Content
var body: some View {
// use `content` somewhere in your code
content
// ... (other customizations)
}
}Examples

Last updated