3

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
matteok
  • 2,189
  • 3
  • 30
  • 54

3 Answers3

7

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)
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Is there a way to programtically get the visible height of the UINavigationBar i.e. 64.0 in that case? – matteok Oct 23 '16 at 12:34
  • @matteok, check the update, maybe something like that. – Rashwan L Oct 23 '16 at 12:41
  • I just checked and the statusBar frame is 0.0 if the statusbar is hidden so i think checking for isStatusBar hidden is unnecessary. navigationBar.height + statusBarHeight should do the trick in both cases. If you agree you can edit your answer and I'll mark it as correct. – matteok Oct 23 '16 at 12:45
  • Indeed, no need to do the check added it to make it clearer. Updated the answer @matteok. – Rashwan L Oct 23 '16 at 12:47
1

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.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
0

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)
Mig70
  • 61
  • 10