1

I am developing my first iPhone application. I am quite new to iOS. I am trying to get the height of the UI Navigation bar using the code below below the viewDidLoad method

float navBarHeight = self.navigationController.navigationBar.frame.size.height;

But the navBarHeight is returning 0. Not sure why that is.

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
cloudviz
  • 971
  • 4
  • 15
  • 40
  • possible duplicate of [Programmatically get height of navigation bar](http://stackoverflow.com/questions/7312059/programmatically-get-height-of-navigation-bar) – Rachel Gallen May 30 '14 at 22:09
  • If you are on iOS 6 or later (i.e. systems that support Auto Layout) you could try `[navigationBar intrinsicContentSize].height`. – herzbube May 30 '14 at 22:41
  • Where are you calling your code? Is `self` in a `UINavigationController`? – rmaddy May 30 '14 at 23:20
  • That could be it. Self is in UIView I think rather than a UINavigation Controller. I need to check again when I get back to my station. – cloudviz May 30 '14 at 23:36
  • It might be custom UIView instead of UINavigationController.Some where else your hiding navigation bar – Rajesh Dharani Feb 27 '18 at 12:24

3 Answers3

0

Might be your using custom UIView for navigation or your hiding NavigationBar in your code i.e.,why it showing height is 0.If you set below code in your viewWillAppear you will get height of NavigationBar.

[self.navigationController setNavigationBarHidden:NO];
Rajesh Dharani
  • 285
  • 4
  • 20
0

Try this extension (considering iPhone-X)

extension UIViewController {

    /**
     *  Height of status bar + navigation bar (if navigation bar exist)
     */

    var topbarHeight: CGFloat {
        return UIApplication.shared.statusBarFrame.size.height +
            (self.navigationController?.navigationBar.frame.height ?? 0.0)
    }
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
0

Try :

print(self.navigationController?.navigationBar.frame.height)
Haya Hashmat
  • 137
  • 10