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
Demo.swift
ContentView.swift
App.swift
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)
}
}
Leave a Reply