简体   繁体   中英

UITabBar disappears after pushed to new view controller

I have an UITabBarController that has 3 buttons. The second button points to ViewController1 which is connected to another view called ViewController2 . After I tap a button in ViewController2 I programmatically present ViewController1 again, that works perfect except one thing. After I "arrived" to ViewController1 the tab bar disappears.

I'm using this method to navigate back to ViewController1 . (exactly I navigate to its navigation controller, but already tried with the view)

- (void)presentViewControllerAnimated:(BOOL)animated {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"storyboard" bundle:nil];
    UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:@"destination"];
    [self presentViewController:firstViewNavigationController animated:animated completion:nil];     
}

I call here the first method

- (void)didTapButton:(id)sender {

    UIButton *button = (UIButton *)sender;

    CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];

    [self presentViewControllerAnimated:YES];
}

This method hides the tab bar in the ViewController2 , I already tried without it, therefore there is no problem with it.

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

I can't figure out why this thing happens, I think it's a fair solution, that worked well for a several times when I needed to present views. I've read it can happen with segues, but I'm doing it with code without segues.

Actually your code works right. There should not be tab bar when you present FirstViewController from SecondViewController. Because when you call instantiateViewControllerWithIdentifier its basically creates a new instance of that view controller, and of course, there is no tab bar.

The right way to go back to your first view controller is to pop SecondViewController (or dismiss it, if it presented modally). So your final code should be like this

- (void)didTapButton:(id)sender {
    // If this view controller (i.e. SecondViewController) was pushed, like in your case, then       
    [self.navigationController popViewControllerAnimated:YES];

    // If this view controller was presented modally, then
    // [self dismissViewControllerAnimated:YES completion:nil];
}

And of course, your view controller hierarchy in storyboard must be like this:

-- UINavigationController -> FirstViewController -> SecondViewController
                        | 
->UITabBarController____|
                        -...
                        -...

I've tried the same and got the same result.

My solution was simple, on the push do this :

UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:@"destination"];

firstViewNavigationController.hidesBottomBarWhenPushed = true; // Insert this and set it to what you want to do

[self presentViewController:firstViewNavigationController animated:animated completion:nil];    

and then remove your

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM