SwiftUI Styling Color Schemes
SwiftUI Styling Color Schemes
Use light/dark modes and the system color palette to keep your UI readable and consistent.
Read the current color scheme
Access the environment's color scheme to adapt your UI for light or dark mode.
Example
import SwiftUI
struct ColorSchemesDemo: View {
@Environment(\.colorScheme) var scheme
var body: some View {
Text(scheme == .dark ? "Scheme: Dark" : "Scheme: Light")
.padding()
}
}
import SwiftUI
struct ContentView: View {
var body: some View { ColorSchemesDemo() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
The example above shows the current color scheme.