我的应用程序的很大一部分由web视图组成,以提供尚未通过本机实现提供的功能。网络团队没有计划为网站实施黑暗主题。因此,我的应用程序在iOS 13上的暗模式支持看起来会有点一半一半。

是否可以选择退出暗模式支持,这样我们的应用程序总是显示光模式,以匹配网站主题?


当前回答

我将使用这个解决方案,因为窗口属性可能会在应用程序生命周期中发生变化。因此需要重复分配" overrideuserinterfacstyle = .light"。UIWindow.appearance()使我们能够为新创建的UIWindow对象设置默认值。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

      if #available(iOS 13.0, *) {
          UIWindow.appearance().overrideUserInterfaceStyle = .light
      }

      return true
    }
}

其他回答

最新更新,

如果你用的是Xcode 10。x,那么默认的UIUserInterfaceStyle是light对于iOS 13.x。当在iOS 13设备上运行时,它只能在灯光模式下工作。

不需要显式地在信息中添加UIUserInterfaceStyle键。plist文件,添加它将给出一个错误时,你验证你的应用程序,说:

无效的信息。plist关键。载荷/AppName.appInfo中的键'UIUserInterfaceStyle'。Plist文件无效。

只在Info中添加uiuserinterfacstyle键。plist文件时使用Xcode 11.x。

斯威夫特5

您可以通过以下链接下载演示项目:- https://github.com/rashidlatif55/DarkLightMode

两种方法切换暗到亮模式:

1- info.plist

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>

2-程序化或运行时

  @IBAction private func switchToDark(_ sender: UIButton){
        UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .dark
    }

将此添加到info.plist

<key>UIUserInterfaceStyle</key>
    <string>Light</string>

是的,你可以跳过添加以下代码在viewDidLoad:

if #available(iOS 13.0, *) {
        // Always adopt a light interface style.
        overrideUserInterfaceStyle = .light
    }

除了其他回答,根据我对以下问题的理解,你只需要在对iOS 13 SDK(使用XCode 11)编译时准备Dark模式。

系统假设应用程序链接到iOS 13或更高版本的SDK 支持光明和黑暗的外观。在iOS中,你指定 通过指定特定的界面样式来指定您想要的特定外观 到您的窗口、视图或视图控制器。您也可以禁用支持 黑暗模式完全使用信息。plist关键。

Link