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.
There is an easy way to change the app dark mode theme with animation
Swift 5 (Update):
if let window = UIApplication.shared.windows.first {
UIView.transition (with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.overrideUserInterfaceStyle = .dark //.light or .unspecified
}, completion: nil)
}
Swift 4:
if let window = UIApplication.shared.keyWindow {
UIView.transition (with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.overrideUserInterfaceStyle = .dark //.light or .unspecified
}, completion: nil)
}