Search code examples
objective-cuitabbarcontrolleruitabbartvosapple-tv

TvOS UITabBarController detect tabbar shown/hidden


Is there anyway to detect that the tabbar of a UITabBarController is going to appear or disappear? I want to make an animation simultaneously with the animation that shows/hides the tabbar.

I haven't find any way to detect this event. The property "hidden" of the tabbar is not an option because it changes its value once the animation has finished


Solution

  • The solution was to use the method in the view controller didUpdateFocusInContext:withAnimationCoordinator: with this code:

    static NSString *kUITabBarButtonClassName = @"UITabBarButton";
    
    NSString *prevFocusViewClassName = NSStringFromClass([context.previouslyFocusedView class]);
        NSString *nextFocusedView = NSStringFromClass([context.nextFocusedView class]);
    
        // The tabbar is going to disappear
        if ([prevFocusViewClassName isEqualToString:kUITabBarButtonClassName] &&
            ![nextFocusedView isEqualToString:kUITabBarButtonClassName]) {
            [self.view layoutIfNeeded];
            self.constraintScrollViewCenterY.constant -= self.tabBarController.tabBar.frame.size.height;
    
            [coordinator addCoordinatedAnimations:^{
                [self.view layoutIfNeeded];
            } completion:nil];
        // The tabbar is going to appear
        } else if (![prevFocusViewClassName isEqualToString:kUITabBarButtonClassName] &&
                   [nextFocusedView isEqualToString:kUITabBarButtonClassName]) {
            [self.view layoutIfNeeded];
            self.constraintScrollViewCenterY.constant += self.tabBarController.tabBar.frame.size.height;
    
            [coordinator addCoordinatedAnimations:^{
                [self.view layoutIfNeeded];
            } completion:nil];
        }
    

    where self.constraintScrollViewCenterY is a constraint related to the vertical alignment of the view I want to move according to the tabbar movement

    Note: The use of class name (kUITabBarButtonClassName) instead of [... class] method is due to UITabBarButton is a private class