iOS亮模式和暗模式

时间:2019-12-21 11:17:39

标签: ios swift ios-darkmode

我想在我的应用程序中拥有一项功能。我决定创建UIAlertController和2 AlertAction:亮模式和暗模式。如何通过单击AlertAction之一来更改整个应用程序的模式。请帮助...。

1 个答案:

答案 0 :(得分:0)

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

}