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

前端 未结 16 1434
囚心锁ツ
囚心锁ツ 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
    
    0 讨论(0)
  • 2020-12-08 09:58

    Helper method below that works on any iOS version:

    var isDarkMode: Bool {
        guard #available(iOS 12.0, *) else {
            return false
        }
    
        return UIScreen.main.traitCollection.userInterfaceStyle == .dark
    }
    

    Usage:

    view.backgroundColor = isDarkMode ? .black : .white
    
    0 讨论(0)
  • 2020-12-08 10:01

    For iOS 13, you can use this property to check if current style is dark mode or not:

    if #available(iOS 13.0, *) {
        if UITraitCollection.current.userInterfaceStyle == .dark {
            print("Dark mode")
        }
        else {
            print("Light mode")
        }
    }
    
    0 讨论(0)
  • 2020-12-08 10:01

    As mentioned by daveextreme, checking the current view user interface style doesn't always return the system style when you use the overrideUserInterfaceStyle property. In such cases it may be better to use the following code:

    switch UIScreen.main.traitCollection.userInterfaceStyle {
    case .light: //light mode
    case .dark: //dark mode
    case .unspecified: //the user interface style is not specified
    }
    
    0 讨论(0)
  • 2020-12-08 10:02

    Create a class function for write method 1 time and use everywhere you want

    func isDarkMode() -> Bool{
        if #available(iOS 12.0, *) {
            if UIScreen.main.traitCollection.userInterfaceStyle == .dark {
                return true
            } else {
                return false
            }
        } else {
           return false
        }
    }  
    
    0 讨论(0)
  • 2020-12-08 10:04

    You should check the userInterfaceStyle variable of UITraitCollection, same as on tvOS and macOS.

    switch traitCollection.userInterfaceStyle {
    case .light: //light mode
    case .dark: //dark mode
    case .unspecified: //the user interface style is not specified
    }
    

    You should use the traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) function of UIView/UIViewController to detect changes in the interface environment (including changes in the user interface style).

    From Apple Developer Documentation:

    The system calls this method when the iOS interface environment changes. Implement this method in view controllers and views, according to your app’s needs, to respond to such changes. For example, you might adjust the layout of the subviews of a view controller when an iPhone is rotated from portrait to landscape orientation. The default implementation of this method is empty.

    System default UI elements (such as UITabBar or UISearchBar) automatically adapt to the new user interface style.

    0 讨论(0)
提交回复
热议问题