How to set @Environment(\.colorScheme) back to system/automatic?

Viewed 657

I'm trying to add a view in my app settings page where the users can choose to force dark mode, light mode or keep the system behavior.

If I add .environment(\.colorScheme) to the ContentView it works fine in case I force one of the two themes, but since the available options are only .light or .dark how do I get back to .automatic?

I also tried using a conditional modifier for the color scheme, which works, but the problem is that it ends up reloading the all app and it causes the app to jump form settings to the home screen.

2 Answers

You can use preferred color scheme, like in below example

VStack {
   // ... some other code
}
.preferredColorScheme(someStateFlag ? .dark : nil)   // on some condition reset

For me this works fine

YourView
  .preferredColorScheme(
     AppEnvironment.environment == .develop ? ColorScheme(.unspecified) : .light
  ) 

Note: You can add any condition instead of AppEnvironment.environment == .develop

Related