Modifiers
Modifiers return new views with applied changes such as padding, font, and color.
Example
Demo.swift
ContentView.swift
App.swift
import SwiftUI
struct ModifiersDemo: View {
var body: some View {
Text("Hello")
.font(.title)
.foregroundStyle(.blue)
.padding()
.background(.blue.opacity(0.1))
.cornerRadius(8)
}
}
ViewBuilder
Use @ViewBuilder to compose multiple child views in a single return position.
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