60

I've found out that the height of a UITabBar is 49px (or 50px, depending on the source).

Because I don't like to use too much hard-coded values in my code I was wondering if it is possible to retrieve the height of the tabbar programmatically.

Kind regards,
Niels R.

PS: The reason I'm asking is because I have a view controller (with a list view containing text fields) that is either simply pushed by the navigationcontroller (pushViewController) or presented as a modal (presentModalViewController). As soon as the keyboard show up, the size of the view is reduced, but I have to take into account that the tabbar is only visible when the view controller is pushed and not presented as a modal.

shim
  • 9,289
  • 12
  • 69
  • 108
Niels R.
  • 7,260
  • 5
  • 32
  • 44

8 Answers8

149

I don't totally understand your P.S., but you can do:

tabBarController?.tabBar.frame.size.height
shim
  • 9,289
  • 12
  • 69
  • 108
Mason
  • 3,577
  • 2
  • 20
  • 11
14

If you want to get the standard height for the UITabBar control then you can do:

    UITabBarController *tabBarController = [UITabBarController new];
    CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
Federico Jordan
  • 186
  • 1
  • 6
  • 2
    I love this answer. This is my favorite answer. I recommend this answer if you are using Objective C – Brian Whipp Mar 30 '17 at 03:40
  • 5
    This is a bad answer though.. You create a completely new UITabBarController from scratch, which is entirely unnecessary and wasteful. If you need the height of a TabBar, you should get the height of your current tabBar. – Sti Sep 21 '17 at 13:33
  • Awesome also fixed iPhone X wrong tabbar size problem on first app install. – ergunkocak May 04 '19 at 13:47
10

prettier

CGRectGetHeight(self.tabBarController.tabBar.frame)
Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
  • Thank you. And guided by xcode 8 beta it changed it to 'let tabBarHeight = (self.tabBarController?.tabBar.frame)!.height' – kometen Jun 27 '16 at 12:12
  • @kometen - be conscious of the force unwrap (ie: `!`) there, it can cause unwanted crashes – NSTJ Nov 19 '16 at 05:37
6

check this in your vc

bottomLayoutGuide.length
Adam Smaka
  • 5,977
  • 3
  • 50
  • 55
3

In Swift 3, you can retrieve the height of the UITabBar with this code below.

self.tabBarController!.tabBar.frame.size.height

Which will return a CGFloat value of the height

Cyril
  • 2,783
  • 1
  • 24
  • 35
1

If an object is based on UIView (which most visual elements in the library are), you can get the size from the ivar "frame".

Hack Saw
  • 2,741
  • 1
  • 18
  • 33
1

UITabBar is inherited from UIView as long as you mhave access to your UITabBar instance you can access and modify the frame of UITabBar,

CGRect myRect = myTabBar.frame;
Maulik
  • 19,348
  • 14
  • 82
  • 137
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

Try to use the intrinsicContentSize property of the tab bar like this

let tabBarHeight = self.tabBarController.tabBar.intrinsicContentSize.height
iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23