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

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

提交回复
热议问题