Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide TabBar when pushed into the navigation stack and bring it back when popped out of the navigation stack

I am trying to do the following.

I have a tab bar controller with 2 tabs in it. Both the tabs are navigation controller with a table view on each of them.

Now when I select one cell of the table in the first tab, I am pushing another tab bar controller, so I would like to hide the tab bar of the parent tabbarcontroller, and when I click the back button on the navigation bar I would like to see the the parent tab bar again, as I am in my parent tab bar view.

I tried hidesbottombarwhenpushed and it hides the parent tab bar controller tab bar but when I click back it doesn't brings it back.

like image 997
Yogesh Avatar asked Feb 13 '11 04:02

Yogesh


2 Answers

The accepted answer had a problem to me.

My app had a navigation with the depth of three UIViewController.

  • The FirsViewController show's the UITabBar. (Correct)
  • The FirsViewController pushes the SecondViewController, and the SecondViewController don't show's the UITabBar. (Correct)
  • The SecondViewController pushed the ThirdViewController, and the ThirdViewController show's the UITabBar. (Incorrect)
  • The ThirdViewController popped to the SecondViewController, and the SecondViewController show's the UITabBar. (Incorrect)
  • The SecondViewController popped to the FirstViewController, and the FirstViewController show's the UITabBar. (Correct)

The solution for me was setting the delegate of UINavigationControllerDelegate

swift:

self.navigationController?.delegate = self

Objective-c:

self.navigationController.delegate = self;

And then implement the following delegate method

Swift:

fun navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    if fromVC.isKindOfClass(FirstViewController) && toVC.isKindOfClass(SecondViewController) {

        self.hidesBottomBarWhenPushed = true;

    }
    else if fromVC.isKindOfClass(SecondViewController) && toVC.isKindOfClass(FirstViewController) {

        self.hidesBottomBarWhenPushed = false;

    }

    return nil

}

Objective-c:

-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                 animationControllerForOperation:(UINavigationControllerOperation)operation
                                              fromViewController:(UIViewController*)fromVC
                                                toViewController:(UIViewController*)toVC
{

    if ([fromVC isKindOfClass:[FirstViewController class]] && [fromVC isKindOfClass:[SecondViewController class]]) {

        self.hidesBottomBarWhenPushed = true;

    }
    else if ([fromVC isKindOfClass:[SecondViewController class]] && [fromVC isKindOfClass:[FirstViewController class]]) {

        self.hidesBottomBarWhenPushed = false;

    }

    return nil;

}

Hope it helped.

like image 70
Bruno Coelho Avatar answered Sep 17 '22 12:09

Bruno Coelho


Ok, So finally I got my answer, this is what I am suppose to do.

self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:aViewController animated:YES];
self.hidesBottomBarWhenPushed=NO;

So basically hidesBottomBarWhenPushed = YES, and then push your view controller and then hidesBottomBarWhenPushed = NO; this works like a charm.

Thanks to eddy and his post here

like image 32
Yogesh Avatar answered Sep 19 '22 12:09

Yogesh