ios - 获取 iPhone 最新型号的底部高度

标签 ios swift

我有一个函数可以计算屏幕高度的 1/3。这是实现:

struct Offsets {

  static let singlePixel = (1.0 / UIScreen.main.scale)
  static func  thirdScreenHeightExceptTabAndNavBar() -> CGFloat {
    let screenHeight = UIScreen.main.bounds.size.height
    let screenHeightExceptStatusAndNavAndTabBar = screenHeight - Offsets.statusBarHeight() - Offsets.navBarHeight() - Offsets.tabBarHeight()
    return CGFloat(screenHeightExceptStatusAndNavAndTabBar / 3)

  }

  static func navigationAndStatusBarHeight() -> CGFloat{
    return Offsets.statusBarHeight() + Offsets.navBarHeight()
  }

  static func tabBarHeight() -> CGFloat {
    return UITabBarController().tabBar.frame.size.height
  }

  static func navBarHeight() -> CGFloat {
    return UINavigationController().navigationBar.frame.size.height
  }

  static func statusBarHeight() -> CGFloat {
    return UIApplication.shared.statusBarFrame.size.height
  }
}

打印时,标签栏高度为 49,但是,看起来标签栏顶部到设备底部的距离更大,有屏幕。

enter image description here

如何测量这个距离?

最佳答案

您可以使用 UIApplication 的 shared 中 keyWindowsafeAreaInsets 计算屏幕的 bottomInset 实例。

if #available(iOS 11.0, *) {
    let bottom = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom
    print(bottom)
} 

关于ios - 获取 iPhone 最新型号的底部高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56865758/

相关文章:

ios - Google OAuth token 在一段时间后停止工作(iOS 7 应用程序)

iOS RxSwift 4.2 如何使用 DelegateProxy 实现 CBCentralManager 的 Reactive 扩展?

design-patterns - 协议(protocol):为什么一致性检查和可选要求需要@ObjC?

swift - 在 Swift 中创建一类全局常量的最佳方法是什么

iOS VideoPreview 横向颠倒

ios - iCloud 与核心数据和文件同步

ios - Autolayout - 添加和运行约束

ios - 调用中的额外参数 "animated"- 错误

ios - 如何在没有 ( ) 或 - swift 3 的情况下删除电话号码?

swift - 快速更改标签栏未选择的图标颜色