可以检测到任一用户从xcode 10.x在iPhone上选择了暗模式



我仍在使用 xcode 10.2.1,由于其他一些问题尚未升级到 xcode 11。现在我想检测使用 iOS 13 的用户是否选择了深色模式或浅色模式作为他们的应用程序设置。

根据苹果文档,如果开发人员通过以前的 xcode 构建应用程序,该应用程序默认处于浅模式,这是我的情况,很好。

那么,有没有办法检测用户当前的外观模式。

我正在使用的代码片段:

if #available(iOS 13.0, *) {
guard(traitCollection.responds(to: #selector(getter: UITraitCollection.userInterfaceStyle)))
else { return }
let style = traitCollection.userInterfaceStyle
switch style {
case .light:
print("light")
case .dark:
print("dark")
case .unspecified:
print("unspecified")
@unknown default:
print("unspecified")
}
}

但它总是返回未指定或轻盈的返回。

您可以使用此属性来检查当前样式是否为深色模式:

if #available(iOS 13.0, *) {
if UITraitCollection.current.userInterfaceStyle == .dark {
print("Dark mode")
}
else {
print("Light mode")
}
}

最新更新