In this app, I set the preferredColorScheme in main
@main
struct TheApp: App {
@AppStorage("isDarkMode") private var isDarkMode = false
var body: some Scene {
WindowGroup {
ContentView()
.preferredColorScheme(isDarkMode ? .dark : .light)
}
}
}
isDarkMode is changed dynamically in code.
When code is compiled with Xcode 15.3, the background of ContentView changes from light to dark or vice versa.
When compiled with Xcode 16, no more change.
However, direct changes to objects do work, such as:
TextField("N", value: $t, format: .number)
.frame(width: 40)
.background(isDarkMode ? .gray : .white)
What has changed in Xcode 16 ? Is it documented somewhere ?
I saw this SO thread which describe similar problem. https://www.reddit.com/r/swift/comments/upkprg/preferredcolorscheme_toggle_not_working/
Thanks for the test @GRALInfo !
@Claude31 due to the info that both you and @GRALInfo have provided, it seems like this is a known issue that has been fixed in macOS 15. Since you cannot update, could you try putting the User Defaults value into an observable class and manually writing a getter and setter and see if that works?
I saw some folks chatting about this here: https://github.com/sindresorhus/Settings/issues/117, maybe check that out.