Hide tvOS 13 tab bar when not in focus

1.3k views Asked by At

Prior to TvOS 13, TvOS 12 uses a auto hiding tab bar whenever it's not in focus. But in TvOS 13, it uses a fixed tab bar, which is always visible at the top.

The fixed tab bar does not fit my app requirement, because I need to display my tab content in full screen with the tab bar hidden away.

Are there anyway I can change the tab bar back to the previous version, such that it will always be hidden when not in use, and I can simply just swipe up to show the tab bar menu anytime?

2

There are 2 answers

0
Matteo Gobbi On

No you can’t. However if you have a scroll view, you can connect the menu to the scroll view such that the menu scrolls menu when you scroll through the scroll view and reappear when you scroll back up.

Check tabBarObservedScrollView https://developer.apple.com/documentation/uikit/uiviewcontroller/3152928-tabbarobservedscrollview

0
Vilius Paulauskas On

You can implement it fairly easily. The idea is:

  1. Create a method that animates your TabBar out of screen
  2. Start this animation when focus goes out of your TabBar

Here's the code for UITabBarController:

// Animate UITabBar out of screen bounds
@implementation YourTabBarController {
    BOOL _isHidden;
    CGFloat _originalY;
}

-(void)setTabBarHidden:(BOOL)hidden {
    if (hidden == _isHidden) {
        return;
    }
    
    CGRect frame = self.tabBar.frame;
    CGFloat alpha;
    if (hidden) {
        // Calculate original distance from top of the window
        _originalY = [self.tabBar convertPoint:frame.origin toView:nil].y;
        frame.origin.y -= (frame.size.height + _originalY);
        // ! Important not to use 0, otherwise your TabBar will be
        // removed from focus engine and will not re-appear.
        // focus engine.
        alpha = 0.1;
    } else {
        frame.origin.y += (frame.size.height + _originalY);
        alpha = 1;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.tabBar.frame = frame;
        self.tabBar.alpha = alpha;
    }];
    
    _isHidden = hidden;
}

-(void)didUpdateFocusInContext:(UIFocusUpdateContext *)context 
      withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
    if (context) {
        id<UIFocusItem> item = [context nextFocusedItem];
        
        if (item != nil && [item respondsToSelector:@selector(isDescendantOfView:)]) {
            BOOL isTabBarFocused = [(UIView*)item isDescendantOfView:self.tabBar];
            
            [self setTabBarHidden:!isTabBarFocused];
        }
    }
}