matteok
matteok

Reputation: 2189

UINavigationBar frame height returns 44.0 but is actually 64.0

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.

Upvotes: 3

Views: 1806

Answers (3)

Mig70
Mig70

Reputation: 61

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)

Upvotes: 0

Rashwan L
Rashwan L

Reputation: 38833

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)

Upvotes: 8

Léo Natan
Léo Natan

Reputation: 57050

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.

Upvotes: 1

Related Questions