iOS light mode and dark mode

I want to have one feature in my app. I decided to create UIAlertController and 2 AlertAction: Light Mode and Dark Mode. How I can change mode in whole my app by clicking one of my AlertAction. Please Help ....

Upvotes: 1

Views: 650

Answers (1)

Celeste
Celeste

Reputation: 1577

@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)

}

Upvotes: 3

Related Questions