UINavigationBar frame height returns 44.0 but is actually 64.0

回眸只為那壹抹淺笑 提交于 2020-01-02 10:20:00

问题


I have a UINavigationController in a storyboard which is not using AutoLayout. With the status bar visible the actual height of the UINavigationBar is 64.0 and yet when I log self.navigationBar.frame.size.height I get 44.0. How do I get the actual height of the UINavigationBar?

I'm using Xcode 7.3 and the storyboard builds for iOS 6.


回答1:


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)



回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/40202904/uinavigationbar-frame-height-returns-44-0-but-is-actually-64-0

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