SwiftUI ViewBuilder
Use @ViewBuilder to compose multiple child views in a single return position.
Compose rows with @ViewBuilder
Extract a reusable row builder using @ViewBuilder and combine multiple child views without extra containers.
Example
Demo.swift
ContentView.swift
App.swift
import SwiftUI
@ViewBuilder
func InfoRow(_ title: String, _ value: String) -> some View {
HStack { Text(title).bold(); Spacer(); Text(value) }
}
struct ViewBuilderDemo: View {
var body: some View {
VStack(spacing: 8) {
InfoRow("Name", "SwiftUI")
InfoRow("Version", "5+")
}
.padding()
}
}
Leave a Reply