UINavigationBar frame height returns 44.0 but is actually 64.0

三世轮回 提交于 2019-12-06 05:57:21

The height of the UINavigationBar is 44. The reason you´re getting 64 is because of your status bar is visible and it has a height of 20.

Update:
To calculate the height you could:

let height = Double(UIApplication.shared.statusBarFrame.height) + Double(self.navigationController!.navigationBar.frame.height)

Do not use magic numbers. Use the view controller's topLayoutGuide.length to get the correct height. Navigation bar height and status bar height can change during runtime, so run your code in viewDidLayoutSubviews to always use the correct value.

The cleanest way is to just care of the navigationBar position and height and not assume there is a status bar over it. (As a matter of fact if the phone orientation is landscape there won't even be a status bar. Just do this:

let height = Double(self.navigationController!.navigationBar.origin.y) + Double(self.navigationController!.navigationBar.frame.height)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!