I am trying to change the color scheme of a sheet in SwiftUI on a button click and use @AppStorage to store the selected scheme. It works exactly two times when clicking the button but after that the color scheme won't change anymore. I'm testing on iOS 15 if that matters. I noticed the print-statement in the onChanged of the @AppStorage property is also only called two times. Here is a minimal example that reproduces the problem:
import SwiftUI
struct settingsView: View {
@State private var showSheet = false
@AppStorage("isDarkMode") private var isDarkMode = false
var body: some View {
Button("Show Sheet") {
showSheet.toggle()
}
.sheet(isPresented: $showSheet) {
Button("Toggle Color scheme") {
isDarkMode.toggle()
}
.preferredColorScheme(isDarkMode ? .dark : .light)
.onChange(of: isDarkMode) { newValue in
print("isDarkMode changed to \(newValue)")
}
}
}
}