Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取iPhone最新型号底部高度_Ios_Swift - Fatal编程技术网

Ios 获取iPhone最新型号底部高度

Ios 获取iPhone最新型号底部高度,ios,swift,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 screenHeightExc

我有一个函数,假设计算屏幕高度的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,但看起来标签栏顶部到设备底部的距离更大,屏幕上没有显示


如何测量此距离?

您可以获得如下所示的偏移。新槽口使用safeAreaLayout显示,因此您可以根据它进行计算

if #available(iOS 11.0, *) {
  let bottom = view.safeAreaInsets.bottom
}

您可以使用
ui应用程序的
共享
实例中
keyWindow
safeAreaInsets
来计算屏幕的
bottomInset

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

我的类没有视图的引用。请尝试从窗口获取俯视图。窗口可从UIC应用程序访问