Navigation controller isn't hiding tabbar when hidesBottomBarWhenPushed = YES on state restoration

StackOverflow https://stackoverflow.com/questions/23105057

  •  04-07-2023
  •  | 
  •  

Pregunta

I have a problem restoring state for view controller with hidesBottomBarWhenPushed=YES pushed in UINavigationController that placed within UITabBarController.

What happens is basically the UINavigationController stack is restored and the right controller is on screen, but hidesBottomBarWhenPushed is not respected for that controller.

The only viable hack I came up with is the fast tab switch on viewDidAppear to make TabBar disappear as it supposed to happen if hidesBottomBarWhenPushed set:

- (void)_fixTabBarStateRestorationBug {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSInteger currentTab = (NSInteger)self.tabBarController.selectedIndex;
        self.tabBarController.selectedIndex = abs(currentTab - 1);
        self.tabBarController.selectedIndex = currentTab;
    });
}
¿Fue útil?

Solución

This is definitely an iOS bug. Solution above didn't work out-of-the-box for me, but this did if you put this in the tab bar controller's viewDidAppear method:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    NSInteger currentTab = (NSInteger)self.tabBarController.selectedIndex;
    self.tabBarController.selectedIndex = abs(currentTab - 1);
    self.tabBarController.selectedIndex = currentTab;
});

If you put this in the view controllers viewDidAppear for a view controller in a tab you will create an infinite loop. In that cause use the once token approach mentioned in the question.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top