hidesBottomBarWhenPushed = NO not working?

后端 未结 9 1419
南笙
南笙 2020-12-08 14:56

I have a UITabBar in my app, which I\'m hiding on the first UIViewController in the first tab by putting this line in the AppDelegate

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 15:18

    I figured out a really easy to way to solve this by subclassing UINavigationController. You could accomplish the same thing by using a category with an associated object but I already had a subclass so I just threw the code in there.

    First, you add an ivar to your UINavigationController:

    @interface CustomNavigationController ()
    {
        NSMutableSet *_viewControllersWithHiddenBottomBar;
    }
    
    @end
    

    Then I overrode the push and pop methods to take over handling the hiding logic:

    - (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        if(viewController.hidesBottomBarWhenPushed)
        {
            viewController.hidesBottomBarWhenPushed = NO;
            [_viewControllersWithHiddenBottomBar addObject:viewController];
            [self rootViewController].hidesBottomBarWhenPushed = YES;
        }
        else
        {
            [self rootViewController].hidesBottomBarWhenPushed = NO;
        }
        [super pushViewController:viewController animated:animated];
    }
    
    - (UIViewController *) popViewControllerAnimated:(BOOL)animated
    {
        if([_viewControllersWithHiddenBottomBar containsObject:self.viewControllers[self.viewControllers.count - 2]])
        {
            [self rootViewController].hidesBottomBarWhenPushed = YES;
        }
        else
        {
            [self rootViewController].hidesBottomBarWhenPushed = NO;
        }
        UIViewController *poppedViewController = [super popViewControllerAnimated:animated];
        [_viewControllersWithHiddenBottomBar removeObject:poppedViewController];
        return poppedViewController;
    }
    
    - (UIViewController *) rootViewController
    {
        return ((UIViewController *)self.viewControllers.firstObject);
    }
    

    I use the hidesButtomBarWhenPushed property to populate the set and then reset the value at the viewcontroller level (since if any view controller has this property set, everything on top of it will also have it hidden). To make things simple I use the root viewcontroller to control showing and hiding the tabbar based on the values in the set.

    You also need to initialize the set somewhere, I just used initWithRootViewController:.

    This works pretty seamlessly for me and is the least hacky way I could think of to do this, without taking over any existing animations and having to handle edge cases.

提交回复
热议问题