【问题标题】:How to detect if the device (iphone) has physical home button?如何检测设备(iphone)是否有物理主页按钮?
【发布时间】:2018-10-04 18:07:40
【问题描述】:

如何在 Swift 中检测当前设备 (iPhone) 是否具有物理主页按钮,例如:iPhone X、iPhone Xs、iPhone Xs Max、iPhone Xr ?

【问题讨论】:

  • 没有主页按钮 == 安全区域 > 0 返回 true
  • 出于好奇,您为什么想知道设备是否有 Home 键?
  • 查看此问题的公认答案:stackoverflow.com/questions/26028918/… - 可能修改扩展以处理您关心的设备。
  • 额外的高度来自安全区域。所以你不需要知道设备的类型。只需添加安全区域即可。
  • 这是我最初评论的重点。在大多数情况下,当有人想要检测特定设备(或设备类型)时,他们做错了。最好将问题集中在您要解决的实际任务上。

标签: ios iphone swift


【解决方案1】:

检查安全区域:

if @available(iOS 11.0, *), 
    UIApplication.sharedApplication.keyWindow?.safeAreaInsets.bottom > 0 {
    return true
}
return false

Swift 4.2 版本:-

var isBottom: Bool {
    if #available(iOS 11.0, *), let keyWindow = UIApplication.shared.keyWindow, keyWindow.safeAreaInsets.bottom > 0 {
        return true
    }
    return false
}

您还可以检查设备类型(查看此post),但检查安全区域可能是最简单的方法。

【讨论】:

  • Swift 4.2:if #available(iOS 11.0, *), let keyWindow = UIApplication.shared.keyWindow, keyWindow.safeAreaInsets.bottom > 0 { return true } return false
【解决方案2】:

对于 iOS 13 及更高版本:

    var isBottom: Bool {
    if #available(iOS 13.0, *), UIApplication.shared.windows[0].safeAreaInsets.bottom > 0 {
        return true
    }
    return false
}

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2017-07-12
    • 2012-09-18
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多