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/