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

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

提交回复
热议问题