iOS Swift 判断手机是否是刘海屏

要判断当前设备的屏幕是否是刘海屏,可以通过判断手机的屏幕分辨率进行确认,代码如下:

func isLiuHaiPing() -> Bool {
    let screenSize = UIScreen.instancesRespond(to: #selector(getter: UIScreen.main.currentMode)) ? UIScreen.main.currentMode?.size : nil
    if let size = screenSize {
        if !size.equalTo(CGSize(width: 320, height: 480))
            && !size.equalTo(CGSize(width: 640, height:960))
            && !size.equalTo(CGSize(width: 640, height: 1136))
            && !size.equalTo(CGSize(width: 750, height: 1334))
            && !size.equalTo(CGSize(width: 1242, height: 2208)) {
            return true
        }
    }
    return false
}

如果该文章对你有用,麻烦给我个赞, 谢谢!

你可能感兴趣的:(iOS Swift 判断手机是否是刘海屏)