How to animate dark mode change in iOS?
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)
}
iOS light mode and dark mode
@available(iOS 13.0, *)
func changeMode() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "DARK MODE", style: .default, handler: { action in
UIWindow.animate(withDuration: 0.5, animations: {
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .dark
//also try : UIApplication.shared.windows.last?.overrideUserInterfaceStyle = .dark
})
}))
alertController.addAction(UIAlertAction(title: "LIGHT MODE", style: .default, handler: { action in
UIWindow.animate(withDuration: 0.5, animations: {
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
//UIApplication.shared.windows.last?.overrideUserInterfaceStyle = .light
})
}))
self.present(alertController, animated : true)
}
SwiftUI animate background color change when a condition is met
I have modified your body. Lets try this for easeInOut animation:-
var body: some View {
VStack {
Button {
numberOfTaps += 1
color = (numberOfTaps == 3) ? Color.blue: Color.green
} label: {
Text("Up")
}
Text("\(numberOfTaps)")
.padding()
.background(color.animation(.easeInOut))
Button {
numberOfTaps -= 1
color = (numberOfTaps == 3) ? Color.blue: Color.green
} label: {
Text("Down")
}
}
}
Note:- You can play with different animation property eg. easeIn,
easeOut etc
Manually set light/dark mode in SwiftUI and save users choice
Here is possible approach (scratchy, you can find here on SO property wrapper for defaults and use it for better styling, but the idea to achieve your goal is the same)
Tested with Xcode 11.4 / iOS 13.4
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private(set) static var shared: SceneDelegate?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Self.shared = self
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
// restore from defaults initial or previously stored style
let style = UserDefaults.standard.integer(forKey: "LastStyle")
window.overrideUserInterfaceStyle = (style == 0 ? .dark : UIUserInterfaceStyle(rawValue: style)!)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
...
}
struct ContentView: View {
var body: some View {
// manipulate with style directly in defaults
Toggle(isOn: Binding<Bool>(
get: { UserDefaults.standard.integer(forKey: "LastStyle") !=
UIUserInterfaceStyle.light.rawValue },
set: {
SceneDelegate.shared?.window!.overrideUserInterfaceStyle = $0 ? .dark : .light
UserDefaults.standard.setValue($0 ? UIUserInterfaceStyle.dark.rawValue : UIUserInterfaceStyle.light.rawValue, forKey: "LastStyle")
}
)) {
Text("is Dark")
}
}
}
Related Topics
Default Uifont Size and Weight But Also Support Preferredfontfortextstyle
Custom Uibarbuttonitem with Bg Colour and Text Swift 3
Map Object into 2D Array Swift for Tableview Sections
Problems with Layout of Some Rows in Swiftui List
How to Integrate .Proto Files in Xcode Compilation
App Could Not Authenticate with Facebook and Firebase After Conversion to Swift 3 Syntax
How to Include Openssl on an iOS Project in a Way That Works
Swift - Add Same Navigation Bar Items to Every Page
How to Get Path to a Subfolder in Main Bundle
How to Record and Save at 240 Frames Per Second
Swift: Is Correct to Use Stored Properties as Computed Properties
iOS Framework Does Not Work on Simulator
Resize All Subview in Scrollviewdidzoom
Translate Just 4 Lines of Code from Objective C to Swift (Pointers)
Can't Build an App for Distribution After Joining Existing Enterprise Developer Program