My original solution no longer works on tvOS 9.3, so I found a new one with a subclass of UITabBarController :
@interface TVTabBarController : UITabBarController @property (nonatomic) BOOL useDefaultFocusBehavior; @end @implementation TVTabBarController - (UIView *)preferredFocusedView { return self.useDefaultFocusBehavior ? [super preferredFocusedView] : self.selectedViewController.preferredFocusedView; } - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator { [super didUpdateFocusInContext:context withAnimationCoordinator:coordinator]; self.useDefaultFocusBehavior = YES; } @end ... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tabBarController.tabBar.hidden = YES;
If you are using a storyboard to initially set up your user interface, be sure to set your own TVTabBarController class in your tab bar controller.
Original solution:
The proposed approach with inheritance from UITabBarController did not work for me, because actually -preferredFocusedView is called twice at startup, so I had to add a counter to return self.selectedViewController.preferredFocusedView for the first 2 calls. But this is really a hacker decision, and there is no guarantee that it will not break in the future.
So, I found a much better solution: update focus in appdelegate -applicationDidBecomeActive: on the first call.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tabBarController.tabBar.hidden = YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { static BOOL forceFocusToFirstTab = YES; if (forceFocusToFirstTab) { forceFocusToFirstTab = NO; [self.tabBarController.selectedViewController updateFocusIfNeeded]; } }
kambala
source share