ios - iOS亮模式和暗模式

标签 ios swift ios-darkmode

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

最佳答案

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

}

关于ios - iOS亮模式和暗模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59435702/

相关文章:

ios - CLBeacon : How can I get the distance from the IBeacons?

ios - 即使在黑暗模式下,平台亮度在 iOS 上仍然很亮 - Flutter

ios - 我们如何在 iOS 13 的当前 iOS 应用程序中支持深色模式?

ios - traitCollectionDidChange 仅在后台被调用多次

iphone - 在 iOS 应用程序中使用 PDF 作为矢量图像,保持透明度?

ios - 取消 dropbox loadFile

ios - Expo - 减少 iOS 设备上的 PDF 文件大小

ios - 从 View 中触发淡入淡出标签延迟确实加载而不是在上一个淡入淡出结束时

ios - NSDate:将 UTC 值转换为军事时区值

swift - 如何以编程方式删除 CloudKit "Field Names"?