首先,这是与退出暗模式相关的Apple条目。 此链接的内容适用于Xcode 11和iOS 13:
本部分适用于Xcode 11的用法
如果您希望整个项目不用DarkMode
方法 #1
在您的info.plist文件中使用以下密钥:
UIUserInterfaceStyle
并为其分配值Light。
该XML的UIUserInterfaceStyle分配:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
方法 #2
您可以overrideUserInterfaceStyle
针对应用程序的window
变量进行设置。
根据项目的创建方式,该AppDelegate
文件可能位于文件中,也可能位于中SceneDelegate
。
if #available(iOS 13.0, *) {
window?.overrideUserInterfaceStyle = .light
}
如果您希望逐个退出UIViewController
override func viewDidLoad() {
super.viewDidLoad()
// overrideUserInterfaceStyle is available with iOS 13
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
}
Apple文档overrideUserInterfaceStyle
上面的代码在Xcode 11中的样子:
本部分适用于Xcode 10.x的用法
如果您使用Xcode 11进行提交,则可以放心忽略此行下的所有内容。
由于相关的API在iOS 12中不存在,因此在尝试使用上面提供的值时会出现错误:
对于设置overrideUserInterfaceStyle
在UIViewController
如果您希望逐个退出UIViewController
这可以通过测试编译器版本和iOS版本在Xcode 10中进行处理:
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
#endif
如果您希望退出整个申请
您可以通过将以下代码添加到AppDelegate文件中来修改上述代码段,以使其适用于Xcode 10的整个应用程序。
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
window?.overrideUserInterfaceStyle = .light
}
#endif
但是,使用Xcode 10.x版时,plist设置将失败:
参考
https://juejin.im/post/5d401f6ef265da03dc07495e
https://stackoverflow.com/questions/56537855/is-it-possible-to-opt-out-of-dark-mode-on-ios-13