我正在使用模拟器来测试iphone x上的所有设备,我一直在寻找能够检测到iPhone x的方法,但它们始终始终返回false
这是其中一种方法
struct Device {
// iDevice detection code
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_RETINA = UIScreen.main.scale >= 2.0
static let SCREEN_WIDTH = Int(UIScreen.main.bounds.size.width)
static let SCREEN_HEIGHT = Int(UIScreen.main.bounds.size.height)
static let SCREEN_MAX_LENGTH = Int( max(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let SCREEN_MIN_LENGTH = Int( min(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let IS_IPHONE_4_OR_LESS = IS_IPHONE && SCREEN_MAX_LENGTH < 568
static let IS_IPHONE_5 = IS_IPHONE && SCREEN_MAX_LENGTH == 568
static let IS_IPHONE_6 = IS_IPHONE && SCREEN_MAX_LENGTH == 667
static let IS_IPHONE_6P = IS_IPHONE && SCREEN_MAX_LENGTH == 736
static let IS_IPHONE_X = IS_IPHONE && SCREEN_MAX_LENGTH == 812
}
这是另一种方法
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 1136:
print("iPhone 5 or 5S or 5C")
case 1334:
print("iPhone 6/6S/7/8")
case 2208:
print("iPhone 6+/6S+/7+/8+")
case 2436:
print("iPhone X")
retValue = true
default:
print("unknown")
}
}
为什么所有方法都返回false?是因为我使用模拟器还是什么?
答案 0 :(得分:6)
使用以下方法检测iPhone X:
var iphoneX = false
if #available(iOS 11.0, *) {
if ((UIApplication.shared.keyWindow?.safeAreaInsets.top)! > CGFloat(0.0)) {
iphoneX = true
}
}
答案 1 :(得分:0)
enum DeviceType {
case iPhoneX
case iPhone5
case iPhone6Plus
case iPhone6
case Unknown
}
class Utils {
class var isIphoneX:Bool {
if deviceType() == .iPhoneX {
return true
}
return false
}
class func deviceType()->DeviceType {
switch UIScreen.main.nativeBounds.height {
case 1136:
return .iPhone5
case 1334:
return .iPhone6
case 1920, 2208:
return .iPhone6Plus
case 2436:
return .iPhoneX
default:
return .Unknown
}
}
}
并像这样使用它:
if Utils.isIphoneX {
//Do things
}