3

In one of my controller i use custom toolbar, i want to get height of default toolbar (navigation bar) in ios. I used macro #define NAVBAR_HEIGHT self.navigationController.navigationBar.frame.size.height.

However, when controller is not embed in navigation stack that macro returns zero.

I dont want to use constants of 44, because it may be different on other Apple device.

For tab bar i use:

+(CGFloat)getTabBarHeight{

    UITabBarController *tabBarController = [UITabBarController new];
    CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
    return tabBarHeight;
}

Is there any snippet like that, so i can return value of navigation controller toolbar?

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107

1 Answers1

15

I'm not sure about Objective C - but in Swift 3 / 4 you can do

func getNavBarHeight() -> CGFloat {
    return self.navigationController?.navigationBar.frame.size.height
}

This requires you to have your viewcontroller embedded in UINavigationController. If you do not have this, you can do:

func getNavBarHeight() -> CGFloat {
    let nav = UINavigationController()
    return nav.navigationBar.frame.size.height
}
David Stenstrøm
  • 762
  • 1
  • 8
  • 22