简体   繁体   中英

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 ....

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM