我仍然在使用Xcode10.2.1,由于其他一些问题,我还没有升级到Xcode11。现在,我想要检测使用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")
}
}但它总是返回未指定的或轻量级的。
发布于 2019-10-14 19:22:24
可以使用此属性检查当前样式是否为暗模式:
if #available(iOS 13.0, *) {
if UITraitCollection.current.userInterfaceStyle == .dark {
print("Dark mode")
}
else {
print("Light mode")
}
}https://stackoverflow.com/questions/58375554
复制相似问题