【问题标题】:userInterfaceIdiom returning wrong values on real devicesuserInterfaceIdiom 在真实设备上返回错误值
【发布时间】:2016-05-05 22:28:39
【问题描述】:
我的应用被拒绝,因为我的应用的 iPad UI 出现问题。在被拒绝之前,我通过检查设备是否是 iPad 以对 UI 措施进行一些细微调整来修复它。问题是当我打电话时:
UIDevice.currentDevice().userInterfaceIdiom //returns .Unspecified
traitCollection.userInterfaceIdiom //returns -1
在真正的 iPad 上,我得到 .Unspecified。这是为什么呢?
【问题讨论】:
标签:
ios
swift
ipad
uidevice
【解决方案1】:
为了解决这个问题,我创建了一个在真正的 iPad 上实际工作的自定义方法
public extension UIDevice {
func isIpadDevice() -> Bool {
let identifier = deviceIdentifier()
return identifier.lowercaseString.containsString("ipad")
}
private func deviceIdentifier() -> String {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8 where value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
return identifier
}
}
为了使用它调用
UIDevice.currentDevice().isIpadDevice() //wohoo