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

时间:2019-07-03 08:17:04

标签: 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

如何测量此距离?

2 个答案:

答案 0 :(得分:2)

您可以使用<TextBox x:Name="txtMyName1" Margin="3" HorizontalAlignment="Center" VerticalAlignment="Center"/> <TextBox x:Name="txtMyName2" Margin="3" /> bottomInset实例中safeAreaInsets的{​​{1}}来计算屏幕的keyWindow

UIApplication's

答案 1 :(得分:0)

您可以获得如下所示的偏移量。新的缺口显示使用safeAreaLayout,因此您可以从中进行计算。

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