Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate dark mode change in iOS?

Tags:

ios

swift

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.

app setting

like image 799
francisfeng Avatar asked Aug 30 '20 03:08

francisfeng


People also ask

How do you turn on Dark Mod?

Turn on Dark Mode in Settings or Control CenterGo to Settings, then tap Display & Brightness. Select Dark to turn on Dark Mode.

How do I make Xcode dark?

Go to Settings (Preferences) | Appearance & Behavior | Appearance and select Xcode-Dark in the Theme dropdown.

Does SwiftUI have Dark Mode?

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.


1 Answers

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)
}
like image 103
Fadi Abuzant Avatar answered Oct 17 '22 17:10

Fadi Abuzant