如何检查 iOS 暗模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56457395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:52:44  来源:igfitidea点击:

How to Check for iOS Dark Mode?

iosswiftios-darkmode

提问by Marko

? How to observe dark mode state in an iOS app ? How to react to changes in dark mode state in an iOS app

? 如何在 iOS 应用中观察暗模式状态?如何对 iOS 应用中暗模式状态的变化做出反应

回答by Marko

UIKit has had UITraitCollection for a while now. Since iOS 9 you could use UITraitCollection to see whether the device supports 3D Touch (a sad conversation for another day)

UIKit 使用 UITraitCollection 已经有一段时间了。从 iOS 9 开始,您可以使用 UITraitCollection 来查看设备是否支持 3D Touch(另一天的悲伤对话)

In iOS 12, UITraitCollection got a new property: var userInterfaceStyle: UIUserInterfaceStylewhich supports three cases: light, dark, and unspecified

在iOS系统12,UITraitCollection得到了一个新的特性:var userInterfaceStyle: UIUserInterfaceStyle它支持三种情况:lightdark,和unspecified

Since UIViewController inherits UITraitEnvironment, you have access to the ViewController's traitCollection. This stores userInterfaceStyle.

由于 UIViewController 继承了 UITraitEnvironment,您可以访问 ViewController 的traitCollection. 这家商店userInterfaceStyle

UITraitEnviroment also has some nifty protocol stubs that help your code interpret when state changes happen (so when a user switches from the Dark side to the Light side or visa versa). Here's a nice coding example for you:

UITraitEnviroment 也有一些漂亮的协议存根,可以帮助您的代码在状态发生变化时进行解释(因此当用户从黑暗面切换到光明面时,反之亦然)。这是一个很好的编码示例:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.traitCollection.userInterfaceStyle == .dark {
            // User Interface is Dark
        } else {
            // User Interface is Light
        }

    }


    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        // Trait collection has already changed
    }

    override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
        // Trait collection will change. Use this one so you know what the state is changing to.
    }
}

回答by LinusGeffarth

You can use the following code to check for light, or dark mode in your project:

您可以使用以下代码检查项目中的亮模式或暗模式:

func viewDidLoad() {
    super.viewDidLoad()

    switch traitCollection.userInterfaceStyle {
        case .light, .unspecified:
            // light mode detected
        case .dark:
            // dark mode detected
    }
}

You can also check for changes in the interface style:

您还可以检查界面风格的变化:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)

    let userInterfaceStyle = traitCollection.userInterfaceStyle // Either .unspecified, .light, or .dark
    // Update your user interface based on the appearance
}

Just like in macOS since Mojave, you can define images for both light and dark mode in your asset catalogue so that those images will be used automatically:

就像自 Mojave 以来的 macOS 一样,您可以在资产目录中为明暗模式定义图像,以便自动使用这些图像:

asset catalogues

资产目录

Taken from here.

取自这里