calculating height of UITabBar

IphoneCocoa TouchUikitUiscrollviewUitabbar

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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;

Solution 2 - Iphone

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

Solution 3 - Iphone

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.

Solution 4 - Iphone

Swift 3+

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

It should print 49.0 (Type CGFloat)

Solution 5 - Iphone

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!

Solution 6 - Iphone

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.

Solution 7 - Iphone

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

bottomLayoutGuide.length

Solution 8 - Iphone

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
    }
}

Solution 9 - Iphone

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

let tabBarHeight = self.tabBarController.tabBar.intrinsicContentSize.height

Solution 10 - Iphone

This is how I got it to work in swift 4.1

let tabBarHeight = CGFloat((self.tabBarController?.tabBar.frame.size.height)!)

Solution 11 - Iphone

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

Solution 12 - Iphone

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

Solution 13 - Iphone

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt DelvesView Question on Stackoverflow
Solution 1 - IphoneVladimir ShutyukView Answer on Stackoverflow
Solution 2 - IphonevodkhangView Answer on Stackoverflow
Solution 3 - IphoneGK100View Answer on Stackoverflow
Solution 4 - IphoneAndrewView Answer on Stackoverflow
Solution 5 - Iphonembm29414View Answer on Stackoverflow
Solution 6 - IphonePKCLsoftView Answer on Stackoverflow
Solution 7 - IphoneAlexander BorisenkoView Answer on Stackoverflow
Solution 8 - IphoneTalha RasoolView Answer on Stackoverflow
Solution 9 - IphoneiPhoneDeveloperView Answer on Stackoverflow
Solution 10 - IphoneGraphics FactoryView Answer on Stackoverflow
Solution 11 - IphoneDylan CrombieView Answer on Stackoverflow
Solution 12 - IphonecoderodurView Answer on Stackoverflow
Solution 13 - IphoneMr.SwiftOakView Answer on Stackoverflow