Additional ColorScheme in Swift/SwiftUI beside Dark Mode and Light Mode

591 Views Asked by At

I'm working on an App that usually will be used in dark environments and the light output from the display should be reduced to a minimum. So I want to have a low-contrast with really black backgrounds and grey text. If someone is using that App in another environment the user might consider a usual view with higher contrast - so for example the usual dark or light mode. Of course I can set all backgrounds to black and foregroundColors to gray, but that would require a lot of code to give the user the possibility back to change to usual light / dark mode. MY approach was rather to write an extension to ColorScheme, starting with

public extension ColorScheme {
static let evenDarker = ColorScheme.dark
}

and adjusting the colorvalues from there. In my imaginiation this should have created a copy of .dark. I can call

  let contentView = ContentView()
        .environment(\.colorScheme, .evenDarker)

and it builds without problems, unfortunately it does show the system-setting (as defined on the device) and not a copy of the dark mode. Calling the same view with .dark or .light overrides the system setting though.

Besides I can't find any list or documentation of possible values in the ColorScheme - e.g. how to override the default background color from gray to black. Did anyone ever get this or something similar get to work?

1

There are 1 best solutions below

0
On

In the meantime I found a solution that works for the first question: Working with

public extension UIUserInterfaceStyle {
static let evenDarker = UIUserInterfaceStyle.dark }

and

window.overrideUserInterfaceStyle = .evenDarker

works better, it copies .dark to .evenDarker - and different from using the environment-setting of the ContentView() it also has an option to use .unspecified - if the user wants to use the system setting.