Hi all,
I have a semi-working approach to manually toggle Dark Mode across an app in SwiftUI. The toggle overrides the System dark mode. However, does anyone know how I can add a button for giving an option to revert to System light/dark mode instead?
I am running Xcode beta in iOS14. See code below.
Many thanks!
Settings toggle
Dark mode modifier definition
This is the how I applied the dark mode modifier across the app
I have a semi-working approach to manually toggle Dark Mode across an app in SwiftUI. The toggle overrides the System dark mode. However, does anyone know how I can add a button for giving an option to revert to System light/dark mode instead?
I am running Xcode beta in iOS14. See code below.
Many thanks!
Settings toggle
Code Block @AppStorage("isDarkMode") var isDarkMode: Bool = true var body: some View { Form { Toggle(isOn: $isDarkMode) { Text("Darkmode") } }
Dark mode modifier definition
Code Block public struct DarkModeViewModifier: ViewModifier { @AppStorage("isDarkMode") var isDarkMode: Bool = true public func body(content: Content) -> some View { content .environment(\.colorScheme, isDarkMode ? .dark : .light) .preferredColorScheme(isDarkMode ? .dark : .light) } }
This is the how I applied the dark mode modifier across the app
Code Block @main struct Lab_mate_2App: App { var body: some Scene { WindowGroup { ContentView() .modifier(DarkModeViewModifier()) } } }