如何检测我的设备是否是Swift 4中的iPhoneX? [英] How can I detect if my device is an iPhoneX in Swift 4?

查看:915
本文介绍了如何检测我的设备是否是Swift 4中的iPhoneX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信有更好,更合适的方法来做到这一点。但是现在我正在使用UIScreen.main.bounds来检测我是否正在处理iPhone X(812高)。这个特定的应用程序只是景观,顺便说一句。所以这就是我在这个函数中所拥有的幻灯片视图幻灯片:

I am sure there is a better, more proper way to do this. But right now I am using UIScreen.main.bounds to detect if I am dealing with an iPhone X (812 tall) or not. This specific app is landscape only, btw. So this is what I have in this function where I am crating slides for a slide view:

func setupSlideViews(slideView: [SlideView]) {
    let screenSize = UIScreen.main.bounds

    var frame: CGRect!
    if screenSize.width == 812 {
        frame = scrollView.frame
    } else {
        frame = view.frame
    }
    scrollView.frame = frame
    scrollView.contentSize = CGSize(width: frame.width * CGFloat(slideViews.count), height: frame.height)

    for (i, slideView) in slideViews.enumerated() {
        slideView.frame = CGRect(x: frame.width * CGFloat(i), y: 0, width: frame.width, height: frame.height)
        scrollView.addSubview(slideView)
    }
}

但是如何检查模型?

推荐答案

如果你需要检测设备是否是iPhoneX,请不要使用 bounds ,这取决于设备的方向。因此,如果用户以纵向模式打开您的应用程序,它将失败。您可以使用设备属性 nativeBounds ,它在轮换时不会改变。

If you need to detect if a device is iPhoneX don't use bounds, it depends on the orientation of the device. So if the user opens your app in portrait mode it will fail. You can use Device property nativeBounds which doesn't change on rotation.


In iOS 8及更高版本,屏幕的边界属性将屏幕的界面
方向考虑在内。此行为意味着纵向方向的设备的
范围可能与横向方向上设备的界限
不同。在屏幕尺寸上依赖
的应用可以使用
fixedCoordinateSpace属性中的对象作为他们必须进行的任何
计算的固定参考点。 (在iOS 8之前,屏幕边界
矩形始终反映相对于
纵向方向的屏幕尺寸。将设备旋转到横向或
倒置方向不会改变边界。)

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
    }
}






用法


usage

if UIDevice.current.iPhoneX { 
    print("This device is a iPhoneX")
}

这篇关于如何检测我的设备是否是Swift 4中的iPhoneX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆