Skip to content

Instantly share code, notes, and snippets.

@antokne
Created October 9, 2023 02:29
Show Gist options
  • Save antokne/6bb13b09de4ad9a1f0cd5af93c29913e to your computer and use it in GitHub Desktop.
Save antokne/6bb13b09de4ad9a1f0cd5af93c29913e to your computer and use it in GitHub Desktop.
Test for colorScheme env variable.
import SwiftUI
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme: ColorScheme
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
.onChange(of: colorScheme) { newValue in
print("colorScheme [\(colorScheme)] changed to \(newValue)")
}
}
}
#Preview {
ContentView()
}
@antokne
Copy link
Author

antokne commented Oct 9, 2023

Running and then putting into the background gets the following output:

colorScheme [light] changed to dark
colorScheme [dark] changed to light

or vice versa depending on what your current mode is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment