Users can switch theme in one section of app settings as showed below.
This is done by changing window.overrideUserInterfaceStyle
.
While it works as expected, the change has no animation at all. The whole app goes to the selected style immediately as opposed to the smooth changes in iOS Settings app.
Simply place the assignment in the completion
of UIView.animate(withDuration duration: TimeInterval, animations: @escaping () -> Void, completion: ((Bool) -> Void)? = nil)
won't work.
Turn on Dark Mode in Settings or Control CenterGo to Settings, then tap Display & Brightness. Select Dark to turn on Dark Mode.
Go to Settings (Preferences) | Appearance & Behavior | Appearance and select Xcode-Dark in the Theme dropdown.
SwiftUI lets us detect whether dark mode or light mode is currently enabled using the colorScheme environment key. If you declare this using @Environment , you can refer to it in your views and they will automatically be reloaded when the color scheme changes.
There is an easy way to change the app dark mode theme with animation
if let window = UIApplication.shared.keyWindow {
UIView.transition (with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.overrideUserInterfaceStyle = .dark //.light or .unspecified
}, completion: nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With