iPhone X 的顶部条形高度是多少?

What is the top bar height of iPhone X?

我想知道 iPhone X 顶栏的确切高度。 能否请您提及 X 的 状态栏和导航栏高度。 请帮助我。

The display on iPhone X, however, is 145pt taller than a 4.7" display, resulting in roughly 20% additional vertical space for content.

有关更多信息,您从苹果获得 iphone X 的 HIG documents and detail description in here1 and here2

状态栏高度

之前是20pt,现在是44pt

Because of the sensors on top of the display, the new status bar is split in 2 parts. If your UI is doing something special with that space (previously 20pt high, now 44pt), because it will be taller on the iPhone X. Make sure that it can be dynamically changed in height. A great thing is that the height won’t be changed if a user makes a phone call or is using a navigation app, which was previously the case on other iPhones.

纵向

导航栏高度正常88和大标题时间140

  • 标准标题 - 44pt(状态栏为 88pt)
  • 大标题 - 140pt
  • 底栏 - 34pt

横向

  • 标准标题 - 32pt
  • 底栏 - 21pt

Apple Docs 中没有规范

Apple Docs

根据Geoff Hackworth其88

导航标题类型:

  • 标准标题
  • 大标题

增加导航栏 iOS 11

navigationController?.navigationBar.prefersLargeTitles = true

您可以使用导航栏的.frame 属性 计算出顶部栏区域的整体高度:

Swift 5.0:

let xBarHeight = (self.navigationController?.navigationBar.frame.size.height ?? 0.0) + (self.navigationController?.navigationBar.frame.origin.y ?? 0.0)

对象:

CGRect navbarFrame = self.navigationController.navigationBar.frame;
float topWidth  = navbarFrame.size.width;
float topHeight = navbarFrame.size.height + navbarFrame.origin.y;

我想这有点作弊,但是无论设备如何,添加导航栏的高度及其 y 原点似乎都能给出正确的总高度。

您可以通过在包含的视图控制器中的 view 上使用 safeAreaInsets 以编程方式获取导航栏的高度:

let navBarHeight = view.safeAreaInsets.top

这将说明它是否是大标题导航栏,以及是否附加了搜索栏。

有关详细信息,请参阅 safeAreaInsets documentation

导航栏和往常一样是 44pt(没有大标题时),状态栏从 20pt 增加到 44pt。您可以在调试器中输入以下内容来验证它:

您可以通过以下方式简单地获取它(Swift 3):

let barHeight = navigationController?.navigationBar.frame.maxY

要获得正确的值,请确保在设置 prefersLargeTitles

后调用它
navigationController?.navigationBar.prefersLargeTitles = false

如果您想知道需要从哪里开始内容,请使用它

extension UIViewController {
  var navigationBarbarContentStart: CGFloat {
    return self.navigationBarTopOffset + self.navigationBarHeight
  }
  var navigationBarTopOffset: CGFloat {
    return self.navigationController?.navigationBar.frame.origin.y ?? 0
  }

  var navigationBarHeight: CGFloat {
    return self.navigationController?.navigationBar.frame.height ?? 0
  }
}

如果您使用的是 UIWindow 并且您需要知道顶部栏的高度,您可以使用这个:

// old way of getting keyWindow
//guard let keyWindow = UIApplication.shared.keyWindow else { return }

// new way of getting keyWindow
guard let keyWindow = UIApplication.shared.windows.first(where: { [=10=].isKeyWindow }) else { return }

let navBarHeight = keyWindow.safeAreaInsets.top

print("navBarHeight:" , navBarHeight)

我的想法来自