这个问题在这里已经有了答案:
Best Answer-推荐答案 strong>
如果您需要检测设备是否为 iPhoneX,请不要使用 bounds
,这取决于设备的方向。因此,如果用户以纵向模式打开您的应用程序,它将失败。您可以使用不会在旋转时更改的设备属性 nativeBounds
。
In iOS 8 and later, a screen’s bounds property takes the interface
orientation of the screen into account. This behavior means that the
bounds for a device in a portrait orientation may not be the same as
the bounds for the device in a landscape orientation. Apps that rely
on the screen dimensions can use the object in the
fixedCoordinateSpace property as a fixed point of reference for any
calculations they must make. (Prior to iOS 8, a screen’s bounds
rectangle always reflected the screen dimensions relative to a
portrait-up orientation. Rotating the device to a landscape or
upside-down orientation did not change the bounds.)
extension UIDevice {
var iPhoneX: Bool {
return UIScreen.main.nativeBounds.height == 2436
}
}
用法
if UIDevice.current.iPhoneX {
print("This device is a iPhoneX")
}
关于ios - 如何在 Swift 4 中检测我的设备是否是 iPhone?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/46633487/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) |
Powered by Discuz! X3.4 |