可以在iOS 13上选择退出暗模式吗? [英] Is it possible to opt-out of dark mode on iOS 13?

查看:121
本文介绍了可以在iOS 13上选择退出暗模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序很大一部分由Web视图组成,以提供本机实现尚不可用的功能. Web团队没有计划为网站实施深色主题.因此,在iOS 13上支持深色模式时,我的应用程序看起来会变成一半/一半.

A large part of my app consists of web views to provide functionality not yet available through native implementations. The web team has no plans to implement a dark theme for the website. As such, my app will look a bit half/half with Dark Mode support on iOS 13.

是否可以选择不支持暗模式,以便我们的应用程序始终显示亮模式以匹配网站主题?

Is it possible to opt out of Dark Mode support such that our app always shows light mode to match the website theme?

推荐答案

首先,这是苹果公司的进入与退出暗模式有关. 此链接的内容是针对Xcode 11& iOS 13 :

First, here is Apple's entry related to opting out of dark mode. The content at this link is written for Xcode 11 & iOS 13:


如果您希望退出整个申请

If you wish to opt out your ENTIRE application

方法1

在您的info.plist 文件中,使用以下密钥:

Approach #1

Use the following key in your info.plist file:

UIUserInterfaceStyle

并为其分配值Light.

UIUserInterfaceStyle分配的 XML :

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

方法2

您可以针对应用程序的window变量设置overrideUserInterfaceStyle.

取决于项目的创建方式,该文件可能位于AppDelegate文件或SceneDelegate中.

Depending on how your project was created, this may be in the AppDelegate file or the SceneDelegate.

if #available(iOS 13.0, *) {
    window?.overrideUserInterfaceStyle = .light
}


如果您希望逐个退出UIViewController

If you wish to opt out your UIViewController on an individual basis

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中的外观:

How the above code will look in Xcode 11:


如果您使用Xcode 11进行提交,则可以放心忽略此行下方的所有内容.

由于相关API在iOS 12中不存在,因此在尝试使用上面提供的值时会出现错误:

Since the relevant API does not exist in iOS 12, you will get errors when attempting to use the values provided above:

用于在UIViewController

如果您希望逐个退出UIViewController

If you wish to opt out your UIViewController on an individual basis

这可以通过测试编译器版本和iOS版本在Xcode 10中进行处理:

This can be handled in Xcode 10 by testing the compiler version and the iOS version:

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

如果您希望退出整个申请

If you wish to opt out your ENTIRE application

通过将以下代码添加到您的AppDelegate文件中,您可以修改上述代码段以使其适用于Xcode 10的整个应用程序.

You can modify the above snippet to work against the entire application for Xcode 10, by adding the following code to your AppDelegate file.

#if compiler(>=5.1)
if #available(iOS 13.0, *) {
    // Always adopt a light interface style.
    window?.overrideUserInterfaceStyle = .light
}
#endif

但是,使用Xcode版本10.x时,plist设置将失败:

@Aron Nelson @Raimundas Sakalauskas @NSLeader rmaddy 的信用,以改善此答案他们的反馈.

Credit to @Aron Nelson, @Raimundas Sakalauskas, @NSLeader and rmaddy for improving this answer with their feedback.

这篇关于可以在iOS 13上选择退出暗模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆