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

前端 未结 16 1432
囚心锁ツ
囚心锁ツ 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:43

    For Swift:

    if #available(iOS 12.0, *) {
      switch UIScreen.main.traitCollection.userInterfaceStyle {
        case .dark: // put your dark mode code here
        case .light: 
        case .unspecified: 
      }
    }
    

    For Objective C:

    if (@available(iOS 12.0, *)) {
            switch (UIScreen.mainScreen.traitCollection.userInterfaceStyle) {
                case UIUserInterfaceStyleDark:
                    // put your dark mode code here
                    break;
                case UIUserInterfaceStyleLight:
                case UIUserInterfaceStyleUnspecified:
                    break;
                default:
                    break;
            }
    }
    

    For more information watch this WWDC2019 video

    0 讨论(0)
  • 2020-12-08 09:43

    You can Easily Detect Dark Mode or Light Mode with this Method Swift 5

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if traitCollection.userInterfaceStyle == .light {
        print("Light mode")
    } else {
        print("Dark mode")
    }}
    
    0 讨论(0)
  • 2020-12-08 09:49

    You can use this extension:

    import UIKit
    
    extension UIApplication {
        @available(iOS 13.0, *)
        var userInterfaceStyle: UIUserInterfaceStyle? {
            return self.keyWindow?.traitCollection.userInterfaceStyle
        }
    }
    
    @available(iOS 13.0, *)
        func setSystemTheme() {
            switch UIApplication.shared.userInterfaceStyle {
            case .dark?:
                currentTheme = .dark
            case .light?:
                currentTheme = .light
            default:
                break
            }
        }
    
    0 讨论(0)
  • 2020-12-08 09:50

    in objective-c you'd want to do:

    if( self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ){
    
            //is dark
    }else{
    
        //is light
    
    }
    
    0 讨论(0)
  • 2020-12-08 09:51

    SwiftUI

    With the \.colorScheme key of an Environment variable:

    struct ContentView: View {
        @Environment(\.colorScheme) var colorScheme
    
        var body: some View {
            Text(colorScheme == .dark ? "In dark mode" : "In light mode")
        }
    }
    

    Also, it automatically updates on the change of the environment color scheme.


    UIKit

    To check the current, all object those conform to UITraitEnvironment protocol, including all UIView subclasses and all UIViewConttroller subclasses have access to the current style:

    myUIView.traitCollection.userInterfaceStyle == .dark
    myUIViewController.traitCollection.userInterfaceStyle == .dark
    

    To detect live changes of the style, here is the full detailed answer

    0 讨论(0)
  • 2020-12-08 09:52

    Some nice extension maybe ?

    public extension UIViewController {
        @available(iOS 12.0, *)
        public var isDarkMode: Bool { traitCollection.userInterfaceStyle == .dark }
    }
    
    0 讨论(0)
提交回复
热议问题