解决方案是;
extension UIDevice {
var modelName: String {
var modelID = ""
#if targetEnvironment(simulator)
modelID = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] ?? ""
#else
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
modelID = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
#endif
return modelID
}
}
我使用 nativeScale 和 scale 参数来检测用户是否打开了显示缩放功能。
if UIScreen.main.nativeScale > UIScreen.main.scale, UIDevice.current.modelName == "iPhone11,8" {
// "iPhone11,8" for iPhone XR
// do nothing here
} else {
// for other devices
tabBarItem.titlePositionAdjustment.vertical = -10.0
}