问题描述:

Here's my situation:

I have a tvOS app where the base navigation is a UITabBarController. Each root view controller referenced from the UITabBarController is a UINavigationController which then handles pushing ViewControllers onto the stack. When a particular ViewController (containing a CollectionView) is active, I need to prevent the default tvOS UITabBarController ability to gain focus.

I've attempted hiding the TabBar manually on ViewWillAppear, Subclassing the TabBar and overriding preferred focus view. In most cases deactivating focus in the TabBar results in focus deactivated in its activeViewController. Currently my solution is to override the "shouldUpdateFocusInContext" delegate method in my ViewController and prevent any focus onto views that aren't UICollectionViews. This works for this one case but is obviously a suboptimal and hack solution.

override func shouldUpdateFocusInContext(context: UIFocusUpdateContext) -> Bool {

guard let nextFocusView = context.nextFocusedView else {return false}

if nextFocusView.isKindOfClass(UICollectionViewCell.classForCoder()) {

return true

} else {

return false

}

}

Anyone have any better ideas of how to temporarily prevent the UITabBarController's TabBar from even displaying when the user swipes up on the tvOS remote?

网友答案:

To prevent the UITabBarController's TabBar from displaying when the user swipes up, use focusHeading:

-(BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext *)context
{
    BOOL result = [super shouldUpdateFocusInContext:context];
    if (context.focusHeading == UIFocusHeadingUp)
        return NO;
    return result;
}

This was especially useful for me because my ViewController doesn't have any focusable views. All it has is a scrollView on the page which used panGestureRecognizer.allowedTouchTypes to capture swipes and I couldn't use the nextFocusedView.

Edit

I also found it useful to use -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item to keep track of which tab was being selected so I could return NO only for certain tabs.

相关阅读:
Top