且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以在iOS 13上选择退出暗模式吗?

更新时间:2023-12-04 19:48:40

首先,这是苹果公司的进入与退出暗模式有关. 此链接的内容是针对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.