How can I check whether dark mode is enabled in iOS/iPadOS?

前端 未结 16 1501
囚心锁ツ
囚心锁ツ 2020-12-08 09:41

Starting from iOS/iPadOS 13, a dark user interface style is available, similar to the dark mode introduced in macOS Mojave. How can I check whether the user has enabled the

16条回答
  •  悲&欢浪女
    2020-12-08 09:52

    var isDarkMode: Bool {
        guard #available(iOS 12.0, *) else {
            return false
        }
        let window = (UIApplication.shared.delegate as? AppDelegate)?.window
        return window?.traitCollection.userInterfaceStyle == .dark
    }
    

    if you are not using window in AppDelegate, call window from SceneDelegate

    It is similar to most answers above, but this works better when we are changing modes using

    window?.overrideUserInterfaceStyle = .dark
    

    can be called as

    isDarkMode ? .black : .white
    

提交回复
热议问题