SwiftUI Styling Theming
SwiftUI Styling Theming
Centralize colors and styles with a theme to keep your app consistent and easy to update.
Centralize styles with a Theme
Define a Theme type to hold colors and spacing, then apply them throughout your views for consistency.
Example
import SwiftUI
struct Theme {
static let primary = Color.blue
static let cardBG = Color.blue.opacity(0.1)
}
struct ThemedCard: View {
var body: some View {
Text("Hello")
.padding()
.background(Theme.cardBG)
.cornerRadius(8)
.foregroundStyle(Theme.primary)
}
}
import SwiftUI
struct ContentView: View {
var body: some View { ThemedCard() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
The example above shows a themed card with a consistent style.