Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    });
}
like image 923
Rob Zombie Avatar asked Apr 16 '14 09:04

Rob Zombie


1 Answers

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.

like image 98
Ben Jackson Avatar answered Oct 19 '22 17:10

Ben Jackson