From df004a4dae99c0b127987ac5d024e6f41eee3581 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:39:50 -0800 Subject: [PATCH 1/2] Contain RNTester's tab bar controller before its view enters a window `RNTesterTabsIOSComponentView` installed its `UITabBarController`'s view as its content view when it was created, so the view entered the window with the component, and the controller became a child of the view controller above only afterwards, in `didMoveToWindow`. UIKit's containment rules ask for the opposite order. On a physical iPhone Duo with iOS 27.2, launched unfolded, RNTester sometimes came up with no tab items until it was sent to the background and back. A likely cause is that the tab bar was laid out once by a controller with no parent and again once it had one. That mechanism is inferred, not confirmed. Install the view only while the controller is a child, in the order UIKit documents for container view controllers: `didMoveToWindow` adds the controller to the view controller above, sizes and installs its view, then calls `didMoveToParentViewController:`, and reverses that when the component leaves the window. Co-Authored-By: Claude Fable 5.2 --- .../ios/RNTesterTabsIOSComponentView.mm | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm b/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm index 8ea5809a7d4a..fae7b0a1faf9 100644 --- a/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm +++ b/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm @@ -79,23 +79,34 @@ - (instancetype)initWithFrame:(CGRect)frame _tabBarController = [UITabBarController new]; _tabBarController.delegate = self; - self.contentView = _tabBarController.view; } return self; } +// The tab bar controller's view is installed only while the controller is a child of the view controller +// above it, as view controller containment requires: +// https://developer.apple.com/documentation/uikit/creating-a-custom-container-view-controller +// A tab bar that the window lays out before its controller has a parent, and again after, can lose its +// items, as the iPhone Duo's side tab bar does. - (void)didMoveToWindow { [super didMoveToWindow]; - if (self.window == nil) { - [_tabBarController willMoveToParentViewController:nil]; - [_tabBarController removeFromParentViewController]; + UIViewController *parentViewController = self.window != nil ? self.reactViewController : nil; + if (_tabBarController.parentViewController == parentViewController) { return; } - UIViewController *parentViewController = self.reactViewController; - if (parentViewController != nil && _tabBarController.parentViewController == nil) { + if (_tabBarController.parentViewController != nil) { + [_tabBarController willMoveToParentViewController:nil]; + self.contentView = nil; + [_tabBarController removeFromParentViewController]; + } + if (parentViewController != nil) { [parentViewController addChildViewController:_tabBarController]; + // Sized before it enters the window, so that its first layout there does not use the controller's + // default full-screen frame. + _tabBarController.view.frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame()); + self.contentView = _tabBarController.view; [_tabBarController didMoveToParentViewController:parentViewController]; } } From 195ec5ed77fc2ea647b95fbca5d158c195337e62 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:40:02 -0800 Subject: [PATCH 2/2] Update RNTester's tab content insets after the tab bar is laid out RNTester's iOS tabs computed their content insets only when the selected tab's safe area changed, from the tab bar's frame at that moment. The safe area can change before the tab bar controller's layout pass moves the bar, so the insets described the bar's previous position. After an unfolded iPhone Duo turned to portrait, where its side tab bar moves to the bottom, the bottom inset stayed 0 and the list ran under the bar, where taps switch tabs. Also compute the insets after each layout pass of the tab bar controller, which a `UITabBarController` subclass reports from `viewDidLayoutSubviews`, and read the safe area at that time instead of taking it from the callback. Co-Authored-By: Claude Fable 5.2 --- .../ios/RNTesterTabsIOSComponentView.mm | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm b/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm index fae7b0a1faf9..2fa847f9982d 100644 --- a/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm +++ b/packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm @@ -26,12 +26,32 @@ static constexpr CGFloat LegacyTopInset = 50; static constexpr CGFloat LegacyBottomInset = 65; +/** + * Reports each of its layout passes, after which the tab bar is where the pass + * put it: along the bottom, along a side, or replaced by a floating bar. + */ +@interface RNTesterTabBarController : UITabBarController +@property (nonatomic, copy) void (^didLayoutSubviews)(void); +@end + +@implementation RNTesterTabBarController + +- (void)viewDidLayoutSubviews +{ + [super viewDidLayoutSubviews]; + if (self.didLayoutSubviews != nil) { + self.didLayoutSubviews(); + } +} + +@end + /** * A tab's view controller, which reports when the tab bar or window changes * the area its content needs to clear. */ @interface RNTesterTabViewController : UIViewController -@property (nonatomic, copy) void (^safeAreaInsetsDidChange)(UIEdgeInsets insets); +@property (nonatomic, copy) void (^safeAreaInsetsDidChange)(void); @end @implementation RNTesterTabViewController @@ -40,7 +60,7 @@ - (void)viewSafeAreaInsetsDidChange { [super viewSafeAreaInsetsDidChange]; if (self.safeAreaInsetsDidChange != nil) { - self.safeAreaInsetsDidChange(self.view.safeAreaInsets); + self.safeAreaInsetsDidChange(); } } @@ -50,7 +70,7 @@ @interface RNTesterTabsIOSComponentView () @end @implementation RNTesterTabsIOSComponentView { - UITabBarController *_tabBarController; + RNTesterTabBarController *_tabBarController; // Holds the mounted React children, and moves into the selected tab. UIView *_containerView; NSArray *_tabKeys; @@ -77,8 +97,12 @@ - (instancetype)initWithFrame:(CGRect)frame _containerView = [UIView new]; _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - _tabBarController = [UITabBarController new]; + __weak RNTesterTabsIOSComponentView *weakSelf = self; + _tabBarController = [RNTesterTabBarController new]; _tabBarController.delegate = self; + _tabBarController.didLayoutSubviews = ^{ + [weakSelf updateContentInsets]; + }; } return self; } @@ -174,8 +198,8 @@ - (void)createTabs:(const std::vector &)tabs image:[UIImage systemImageNamed:RCTNSStringFromString(tab.systemImage)] tag:0]; viewController.tabBarItem.accessibilityIdentifier = RCTNSStringFromString(tab.testID); - viewController.safeAreaInsetsDidChange = ^(UIEdgeInsets insets) { - [weakSelf tabSafeAreaInsetsDidChange:insets]; + viewController.safeAreaInsetsDidChange = ^{ + [weakSelf updateContentInsets]; }; [viewControllers addObject:viewController]; } @@ -217,15 +241,19 @@ - (void)updateEventEmitter:(const EventEmitter::Shared &)eventEmitter { [super updateEventEmitter:eventEmitter]; // The insets can settle before the first event emitter arrives. - UIViewController *selectedViewController = _tabBarController.selectedViewController; - if (selectedViewController.isViewLoaded && selectedViewController.view.window != nil) { - _contentInsets = UIEdgeInsetsZero; - [self tabSafeAreaInsetsDidChange:selectedViewController.view.safeAreaInsets]; - } + _contentInsets = UIEdgeInsetsZero; + [self updateContentInsets]; } -- (void)tabSafeAreaInsetsDidChange:(UIEdgeInsets)insets +// Runs when the selected tab's safe area changes and after each layout pass of the tab bar controller. The +// safe area can change before the pass that moves the tab bar, and the insets depend on where the bar is. +- (void)updateContentInsets { + UIViewController *selectedViewController = _tabBarController.selectedViewController; + if (!selectedViewController.isViewLoaded || selectedViewController.view.window == nil) { + return; + } + UIEdgeInsets insets = selectedViewController.view.safeAreaInsets; CGRect tabBarFrame = _tabBarController.tabBar.frame; CGRect bounds = _tabBarController.view.bounds; BOOL hasBottomTabBar = !_tabBarController.tabBar.hidden && CGRectGetWidth(tabBarFrame) >= CGRectGetWidth(bounds) &&