Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show tab bar after its hidden

Is there any way to show a tab bar after it has been hidden?

Got a tabbar-nav structure. For one of the tabs, I need to hide the tab bar for its 2nd and 3rd level view. But at the same time I will need to show its 1st and 4th view.

The sample code from Elements isn't really applicable here I think.

like image 273
ngzhongcai Avatar asked Aug 23 '10 04:08

ngzhongcai


People also ask

How do I hide and show tab bar in Swift?

Simply, Go to ViewController (in StoryBoard) -> Attribute inspector -> Under 'View Controller' section select 'Hide Bottom Bar on Push' checkbox. This works like a charm.

How do I hide the TabBar in Objective C?

If you don't want that behavior, you should set hidesBottomBarWhenPushed to true where applicable. This will hide the tab bar along with any toolbars you had showing, but only when a view controller is pushed onto the navigation stack. This allows you to show the tab bar at first, then hide it when you need more room.

What is tab bar controller in Swift?

What's A Tab Bar Controller? A tab bar controller, of class UITabBarController, is a container view controller. It typically organizes 3-5 view controllers in a group. The user of your app can switch between view controllers by tapping one of the tabs in the tab bar at the bottom of the screen.

How do I hide the bottom navigation bar in Swift?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


2 Answers

I've found quite a good pragmatic solution to this problem - make the UITabBarController's view larger than it needs to be, so that the actual UITabBar is clipped by the screen.

Assuming that the tab bar view normally fills its superview, this sort of thing should work:

CGRect frame = self.tabBarController.view.superview.frame;
if (isHidden)
{
    CGFloat offset = self.tabBarController.tabBar.frame.size.height;
    frame.size.height += offset;
}
self.tabBarController.view.frame = frame;

The tab bar is still showing, but it's off the bottom of the screen, so appears to have been hidden.

It might have performance implications if it causes extra clipping, but so far, it seems to work.

like image 55
Sam Deane Avatar answered Oct 14 '22 22:10

Sam Deane


The UIViewControllers that are pushed onto the navigation stack can do the something like the following:

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}

EDIT: Added additional code below to deal with the frame. Don't think I particular recommend this idea since it relies on the internal default view structure of a UITabBarController.

Define the following category on UITabBarController:

@interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
@end

@implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
    UITabBar* tabBar = self.tabBar;
    if (show != tabBar.hidden)
        return;
    // This relies on the fact that the content view is the first subview
    // in a UITabBarController's normal view, and so is fragile in the face
    // of updates to UIKit.
    UIView* subview = [self.view.subviews objectAtIndex:0];
    CGRect frame = subview.frame;
    if (show) {
        frame.size.height -= tabBar.frame.size.height;
    } else {
        frame.size.height += tabBar.frame.size.height;
    }
    subview.frame = frame;
    tabBar.hidden = !show;
}
@end

Then, instead of using the tabBar.hidden change I originally suggested, do the following:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController showTabBar:NO];
}

Obviously making sure that the implementation has included the category definition so that 'showTabBar' is known.

like image 43
imaginaryboy Avatar answered Oct 14 '22 22:10

imaginaryboy