diff --git a/lib/ios/RNNBottomTabsController.m b/lib/ios/RNNBottomTabsController.m index 90e545fbc5..d91005d94b 100644 --- a/lib/ios/RNNBottomTabsController.m +++ b/lib/ios/RNNBottomTabsController.m @@ -13,6 +13,8 @@ @implementation RNNBottomTabsController { NSUInteger _previousTabIndex; BottomTabsBaseAttacher *_bottomTabsAttacher; BOOL _tabBarNeedsRestore; + RNNNavigationOptions *_options; + BOOL _didFinishSetup; } - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo @@ -28,6 +30,15 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo _bottomTabsAttacher = bottomTabsAttacher; _bottomTabPresenter = bottomTabPresenter; _dotIndicatorPresenter = dotIndicatorPresenter; + _options = options; + _didFinishSetup = NO; + + IntNumber *currentTabIndex = options.bottomTabs.currentTabIndex; + if ([currentTabIndex hasValue]) { + NSUInteger currentTabIndexValue = [currentTabIndex get]; + _previousTabIndex = currentTabIndexValue; + _currentTabIndex = currentTabIndexValue; + } self = [super initWithLayoutInfo:layoutInfo creator:creator @@ -37,13 +48,6 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo eventEmitter:eventEmitter childViewControllers:childViewControllers]; - IntNumber *currentTabIndex = options.bottomTabs.currentTabIndex; - if ([currentTabIndex hasValue]) { - NSUInteger currentTabIndexValue = [currentTabIndex get]; - _previousTabIndex = currentTabIndexValue; - _currentTabIndex = currentTabIndexValue; - } - if (@available(iOS 13.0, *)) { self.tabBar.standardAppearance = [UITabBarAppearance new]; } @@ -60,6 +64,7 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo action:@selector(handleLongPressGesture:)]; [self.tabBar addGestureRecognizer:self.longPressRecognizer]; + _didFinishSetup = YES; return self; } @@ -132,8 +137,15 @@ - (void)setSelectedIndexByComponentID:(NSString *)componentID { } - (void)setSelectedIndex:(NSUInteger)selectedIndex { - _currentTabIndex = selectedIndex; - [super setSelectedIndex:selectedIndex]; + IntNumber *currentTabIndex = _options.bottomTabs.currentTabIndex; + if ([currentTabIndex hasValue] && !_didFinishSetup) { + NSUInteger currentTabIndexValue = [currentTabIndex get]; + _currentTabIndex = currentTabIndexValue; + } else { + _currentTabIndex = selectedIndex; + } + + [super setSelectedIndex:_currentTabIndex]; } - (UIViewController *)selectedViewController { diff --git a/playground/ios/NavigationTests/RNNCommandsHandlerTest.m b/playground/ios/NavigationTests/RNNCommandsHandlerTest.m index 9ca676635e..4b3e5ff6d9 100644 --- a/playground/ios/NavigationTests/RNNCommandsHandlerTest.m +++ b/playground/ios/NavigationTests/RNNCommandsHandlerTest.m @@ -554,8 +554,7 @@ - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTabWithCustomIndex { completion:^(NSString *componentId){ }]; - // TODO: for some reason the controller always loads the default controller (index 0), regardless the initial value. - XCTAssertTrue(_vc1.isViewLoaded); + XCTAssertFalse(_vc1.isViewLoaded); XCTAssertTrue(_vc2.isViewLoaded); XCTAssertFalse(_vc3.isViewLoaded);