From df004a4dae99c0b127987ac5d024e6f41eee3581 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:39:50 -0800 Subject: [PATCH] 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]; } }