Hide the TabBar in an iOS App

I recently came across a situation where it was useful to have a UITabBar based application, however the actual tab bar was an unnecessary item on the screen. This post is a mini tutorial on how to make this happen.

UPDATE: A few people have asked for the source code, so here is the project.

Inside “application:didFinishLaunchingWithOptions:” method:

tabBarController = [[UITabBarController alloc] init]; 
tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, viewController1, viewController2, nil]; 
[window addSubview:tabBarController.view]; 
[self makeTabBarHidden:TRUE]; 

 

 

Here is the “makeTabBarHidden” method:

-(void)makeTabBarHidden:(BOOL)hide { 
     // Custom code to hide TabBar 
     if ( [tabBarController.view.subviews count] < 2 ) { 
          return; 
     } 

     UIView *contentView;   
     if ( [[tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ) { 
          contentView = [tabBarController.view.subviews objectAtIndex:1]; 
     } else { 
          contentView = [tabBarController.view.subviews objectAtIndex:0]; 
     } 

     if (hide) { 
          contentView.frame = tabBarController.view.bounds; 
     } else { 
          contentView.frame = CGRectMake(tabBarController.view.bounds.origin.x,                
                                         tabBarController.view.bounds.origin.y, 
                                         tabBarController.view.bounds.size.width, 
                                         tabBarController.view.bounds.size.height - 
                                         tabBarController.tabBar.frame.size.height); 
     } 

     tabBarController.tabBar.hidden = hide; 
} 

 

 

And you are probably wondering how to change the views that are normally controlled by the tab bar? Here is the answer…just simply change the selectedIndex property of the tab bar.

 tabBarController.selectedIndex = 1; 

The complete source code for the project.

Part of this code came from a post I found on iPhoneDevSDK.com, so I took it one step further and made it a complete solution.

Email

22 thoughts on “Hide the TabBar in an iOS App

  1. gaurav

    Thanks man for the great solution..
    But i hav one doubt..Do apple vl have any issue with this solution as indirectly we are accessing the internal implementation which is not visible( accessing the subviews of view of tab bar controller) ?? do u know a app which apple have accepted with this piece of code?? I’m waiting for your positive response…Thanks in advance..

  2. Keith

    I found this page on Google whilst trying to hide the tab bar myself. This saved me lots of time/frustration, thank you very much for posting this solution!

  3. Meena

    Hi,
    Its not working with ios 6, It gives exception as “*** Terminating app due to uncaught exception ‘UIViewControllerHierarchyInconsistency’, reason: ‘adding a root view controller as a child of view controller:”.

    Can you please check this issue?

    Thanks.

  4. Gajendra Chauhan

    Hey bro, Its not working and getting crashed. I am using iOS 6. Xcode 4.6. Please fix it as soon as possible.

  5. huy

    Your source code doesnt work :
    2013-08-22 09:41:37.035 HiddenTabBar[787:c07] *** Terminating app due to uncaught exception ‘UIViewControllerHierarchyInconsistency’, reason: ‘adding a root view controller as a child of view controller:’

    Anyway , thanks for the idea man. It helps.

  6. nick Post author

    Since this code was written a while ago, the sample project needed a few things updated. The sample project should now be working again.

  7. Michael

    Nick,

    Because this accesses Apple’s private views without any public API, isn’t it technically against the rules, as gaurav asked above? Even if Apple hasn’t rejected any apps for this reason yet, don’t we risk removal from the app store at any time if we use this method?

    I’m using it right now during development and I’m reluctant to change, but I will have to if there’s any risk of rejection/removal.

    Thanks

  8. nick Post author

    Hi Michael,

    Nothing in this code is accessing a private API. If you feel fearful of rejection then you shouldn’t use this, however this will not be the source of your app’s rejection.

  9. Dean Reyburn

    What’s the reason for returning here? I found this causes a problem in a newer version of iOS:

    if ( [tabBarController.view.subviews count] < 2 ) {
    return;
    }
    If I change the above check to <1 then it works fine. But otherwise iOS overwrites the tab bar icons on my UIView.

    -Dean

  10. nick Post author

    Hi Dean,

    Sorry for the html characters in the code, that should now be fixed. Could you further explain what you are having trouble with?

  11. Dean Reyburn

    My app used tab bar icons for 3 out of 4 windows, but uses that space for other things in one window (it uses an alternative shorter custom tab bar in one window). There are all kinds of issues in iOS 7 with hiding and showing the tab bar. Have you tried the above code in iOS 7 ? Thanks -Dean

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>