Skip to content

Commit

Permalink
Fix Apple TV TabBarIOS issue (facebook#15081)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglowder committed Jul 23, 2017
1 parent 7aec6ae commit 3b2b078
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions React/Views/RCTTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ - (void)reactBridgeDidFinishTransaction
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self.tintColor} forState:UIControlStateSelected];

controller.tabBarItem = tab.barItem;
#if TARGET_OS_TV
// On Apple TV, disable JS control of selection after initial render
if (tab.selected && !tab.wasSelectedInJS) {
self->_tabController.selectedViewController = controller;
}
tab.wasSelectedInJS = YES;
#else
if (tab.selected) {
self->_tabController.selectedViewController = controller;
}
#endif
}];
}

Expand Down Expand Up @@ -175,6 +183,18 @@ - (void)setItemPositioning:(UITabBarItemPositioning)itemPositioning

#pragma mark - UITabBarControllerDelegate

#if TARGET_OS_TV

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(nonnull UIViewController *)viewController
{
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
RCTTabBarItem *tab = (RCTTabBarItem *)self.reactSubviews[index];
if (tab.onPress) tab.onPress(nil);
return;
}

#else

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
Expand All @@ -183,6 +203,8 @@ - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectView
return NO;
}

#endif

#if TARGET_OS_TV

- (BOOL)isUserInteractionEnabled
Expand Down
4 changes: 4 additions & 0 deletions React/Views/RCTTabBarItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
@property (nonatomic, readonly) UITabBarItem *barItem;
@property (nonatomic, copy) RCTBubblingEventBlock onPress;

#if TARGET_OS_TV
@property (nonatomic, assign) BOOL wasSelectedInJS;
#endif

@end
15 changes: 15 additions & 0 deletions React/Views/RCTTabBarItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ - (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_systemIcon = NSNotFound;
#if TARGET_OS_TV
_wasSelectedInJS = NO;
#endif
}
return self;
}
Expand Down Expand Up @@ -118,4 +121,16 @@ - (UIViewController *)reactViewController
return self.superview.reactViewController;
}

#if TARGET_OS_TV

// On Apple TV, we let native control the tab bar selection after initial render
- (void)setSelected:(BOOL)selected
{
if (!_wasSelectedInJS) {
_selected = selected;
}
}

#endif

@end

0 comments on commit 3b2b078

Please sign in to comment.