I have a simple settings view for my MacOS app with a segmented picker for setting the light/dark/automatic(system) mode. It works just fine when switching between the dark and light modes, but when I set it to system (.preferredColorScheme(nil)
), it looks like it initially colors half the app light and half dark. If I click again or click away, it all sets to the proper system mode. But it looks broken on initial selection:
Here's the picker from my settings view:
Picker("",selection: $userSettings.appearance) {
Text("Automatic").tag(0)
Text("Dark").tag(1)
Text("Light").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
.labelsHidden()
It sets a value in my UserSettings:
class UserSettings: ObservableObject {
@AppStorage("appearance") var appearance: Int = 0
var selectedColorScheme: ColorScheme? {
switch appearance {
case 1:
return .dark
case 2:
return .light
default:
return nil
}
}
//other stuff
}
And then I set the actual mode on the outer-most view in my ContentView:
struct ContentView: View {
@StateObject private var userSettings: UserSettings
var body: some View {
NavigationSplitView {
//stuff
} detail: {
//stuff
}
.preferredColorScheme(userSettings.selectedColorScheme)
}
I've also tried to set the .preferredColorScheme
in the main app struct but it doesn't make a difference.
From the last comment in this thread, I also got a sense that this could be a SwiftUI bug.