63

I'm writing an app that uses UITabBar for parts of the navigation. I'm also using UIScrollView for presenting more information than what the screen can typically handle. Because of this, I'm needing to set the scroll view to take into account the height of the UITabBar so that all of the information is displayed.

Is there a way to calculate the height of the UITabBar?

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Matt Delves
  • 1,585
  • 2
  • 13
  • 19

13 Answers13

76

If the view controller has an ancestor that is a tab bar controller, you can retrieve the height from that tab bar.

CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
Kevin
  • 16,696
  • 7
  • 51
  • 68
Vladimir Shutyuk
  • 2,956
  • 1
  • 24
  • 26
69

It is 320 x 49.

If you want to test, open Interface Builder, add a UITabBar, go into the ruler, you will see it

UITabBar is inherited from UIVIew so you can use the frame.size.height to get the height

vodkhang
  • 18,639
  • 11
  • 76
  • 110
  • 13
    Is it guaranteed to stay that way for all time? I would rather use the API to get the value than rely on using IB and inserting magic numbers into the app. – Matt Delves Jun 24 '10 at 03:16
  • UITabBar is inherited from UIView, so you may try with frame.size.height of UIView – vodkhang Jun 24 '10 at 03:18
  • Will give that a shot when I get back from work. Do you know if I can get that without creating an instance of an object? Further, would it be better to create a dummy instance or to pass the in use UITabBar from the application delegate to the view I'm using so that it has a more accurate value? – Matt Delves Jun 24 '10 at 03:22
  • When I use IB, I can not change the height of the UITabBar, so I think it would be fixed. If you want to use API, there is no class method for that:( – vodkhang Jun 24 '10 at 03:34
  • Many thanks. Still to be verified, but enough information to resolve the issue. – Matt Delves Jun 24 '10 at 03:46
  • 8
    Assuming that your app delegate has a tab bar controller variable tabBarController the height can be accessed by ((YouAppDelegate *)UIApplication.sharedApplication.delegate).tabBarController.tabBar.frame.size.height – Matt Delves Jun 24 '10 at 06:48
  • Just an update to Matt Delves solution, there's no need to expose tabBarController member in your app delegate. You can always access tabBarController from a UIViewController object which is a child (or one of its ancestors is a child) of your tabBarController like this: self.tabBarController.tabBar.frame.size.height – CodeBrew Dec 05 '13 at 18:12
  • 320 & 49 are "magic numbers", both confusing and prone to error (example: wrong in iOS7 and on iPad.) It also doesn't answer the question, which was: how to calculate these values (implied: **programmatically**.) As I type this, Vladamir's answer seems best. – Olie Apr 24 '14 at 18:36
  • Hi Olie, I also mention that UITabBar is inherited from UIVIew so you can use the frame.size.height to get the height. So, if anybody who wants to go with promatically, they can go with it :) – vodkhang Apr 29 '14 at 05:25
  • It's still the same height on iOS 9. – ABCD Dec 01 '15 at 14:40
23

In Swift:

let height = self.tabBarController?.tabBar.frame.height ?? 49.0

Relying on the actual height of the tab-bar, and using the magic number as a fallback.

GK100
  • 3,664
  • 1
  • 26
  • 19
10

Swift 3+

let tabBarHeight = tabBarController?.tabBar.frame.size.height
print(tabBarHeight ?? "not defined")

It should print 49.0 (Type CGFloat)

Andrew
  • 36,676
  • 11
  • 141
  • 113
9

I was looking to do something similar with centering a label in the VISIBLE portion of a ViewController's view. This ViewController belonged to a UITabBarController.

Here's the code I used to center my label:

UILabel *roomLabel = [[UILabel alloc] init];
CGRect frame = [[self view] bounds];
float tabBarHeight = [[[super tabBarController] tabBar] frame].size.height;
frame.size.height -= tabBarHeight;
[roomLabel setFrame:frame];
[[self view] addSubview:roomLabel];
[roomLabel release];

Notice that I used [[self view] bounds] not [[self view] frame] because the latter includes the 20 pixel top bar as the Y offset (which throws off the vertical centering).

Hope this helps someone!

By the way: I'm using iOS 4.3 and XCode 4 and the "hard-code" value for the TabBar's height is still 49 for me!

mbm29414
  • 11,558
  • 6
  • 56
  • 87
4

I know this isn't ideal, but I really didn't want to have a magic number constant anywhere. What I did was create a throwaway UITabBarController, and get the height from there.

I did this also because [UITabBar initWithFrame:] works as desired, but doing a [bar setFrame:] doesn't. I needed the frame to be correct at creation.

UITabBarController *dtbc = [[[UITabBarController alloc] init] autorelease];

CGRect tabRect = [[[self navigationController] view] frame];
tabRect.origin.y = tabRect.size.height - [[dtbc tabBar] frame].size.height;
tabRect.size.height = [[dtbc tabBar] frame].size.height;

tabBar_ = [[UITabBar alloc] initWithFrame:tabRect];

What I like about this is that it will correctly place the tab bar at the bottom of the parent regardless of the parents size.

PKCLsoft
  • 1,359
  • 2
  • 26
  • 35
4

This should work in most cases on any instance of UIViewController:

bottomLayoutGuide.length
Alexander Borisenko
  • 1,574
  • 1
  • 12
  • 16
4

In swift 4 and 5. self.tabBarController?.getHeight()

extension UITabBarController{

    func getHeight()->CGFloat{
        return self.tabBar.frame.size.height
    }

    func getWidth()->CGFloat{
         return self.tabBar.frame.size.width
    }
}
Talha Rasool
  • 1,126
  • 14
  • 12
2

Others can also try to get the height using the intrinsicContentSize property of the tab bar.

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

This is how I got it to work in swift 4.1

let tabBarHeight = CGFloat((self.tabBarController?.tabBar.frame.size.height)!)
Aamir
  • 16,329
  • 10
  • 59
  • 65
1

Swift 5

    if let tabBarController = tabBarController {
        let tabBarSafeAreaHeight = tabBarController.tabBar.frame.size.height - 
            tabBarController.tabBar.safeAreaInsets.bottom
    }

This calculates the height of the UITabBar taking into account the safeAreaInsets (UIEdgeInsets)

At the time of writing this equals 49 on iPhone portrait

0
let screenHeight = UIApplication.shared.statusBarFrame.height +
            self.navigationController!.navigationBar.frame.height + (tabBarController?.tabBar.frame.size.height)!

This works perfectly, based it of a few ppls answer here

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
0

SWIFT 5 UPDATE :

AS this thread is old, I am posting here the update from another thread: https://stackoverflow.com/a/25550871/14559220. To sum things up,

in portrait and regular landscape, the height is still 49 points. In compact landscape, the height is now 32 points.

On iPhone X, the height is 83 points in portrait and 53 points in landscape.

Mr.SwiftOak
  • 1,469
  • 3
  • 8
  • 19