Skip to content

Commit 195ec5e

Browse files
ideclaude
andcommitted
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 <noreply@anthropic.com>
1 parent df004a4 commit 195ec5e

1 file changed

Lines changed: 40 additions & 12 deletions

File tree

‎packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,32 @@
2626
static constexpr CGFloat LegacyTopInset = 50;
2727
static constexpr CGFloat LegacyBottomInset = 65;
2828

29+
/**
30+
* Reports each of its layout passes, after which the tab bar is where the pass
31+
* put it: along the bottom, along a side, or replaced by a floating bar.
32+
*/
33+
@interface RNTesterTabBarController : UITabBarController
34+
@property (nonatomic, copy) void (^didLayoutSubviews)(void);
35+
@end
36+
37+
@implementation RNTesterTabBarController
38+
39+
- (void)viewDidLayoutSubviews
40+
{
41+
[super viewDidLayoutSubviews];
42+
if (self.didLayoutSubviews != nil) {
43+
self.didLayoutSubviews();
44+
}
45+
}
46+
47+
@end
48+
2949
/**
3050
* A tab's view controller, which reports when the tab bar or window changes
3151
* the area its content needs to clear.
3252
*/
3353
@interface RNTesterTabViewController : UIViewController
34-
@property (nonatomic, copy) void (^safeAreaInsetsDidChange)(UIEdgeInsets insets);
54+
@property (nonatomic, copy) void (^safeAreaInsetsDidChange)(void);
3555
@end
3656

3757
@implementation RNTesterTabViewController
@@ -40,7 +60,7 @@ - (void)viewSafeAreaInsetsDidChange
4060
{
4161
[super viewSafeAreaInsetsDidChange];
4262
if (self.safeAreaInsetsDidChange != nil) {
43-
self.safeAreaInsetsDidChange(self.view.safeAreaInsets);
63+
self.safeAreaInsetsDidChange();
4464
}
4565
}
4666

@@ -50,7 +70,7 @@ @interface RNTesterTabsIOSComponentView () <UITabBarControllerDelegate>
5070
@end
5171

5272
@implementation RNTesterTabsIOSComponentView {
53-
UITabBarController *_tabBarController;
73+
RNTesterTabBarController *_tabBarController;
5474
// Holds the mounted React children, and moves into the selected tab.
5575
UIView *_containerView;
5676
NSArray<NSString *> *_tabKeys;
@@ -77,8 +97,12 @@ - (instancetype)initWithFrame:(CGRect)frame
7797
_containerView = [UIView new];
7898
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
7999

80-
_tabBarController = [UITabBarController new];
100+
__weak RNTesterTabsIOSComponentView *weakSelf = self;
101+
_tabBarController = [RNTesterTabBarController new];
81102
_tabBarController.delegate = self;
103+
_tabBarController.didLayoutSubviews = ^{
104+
[weakSelf updateContentInsets];
105+
};
82106
}
83107
return self;
84108
}
@@ -174,8 +198,8 @@ - (void)createTabs:(const std::vector<RNTesterTabsIOSTabsStruct> &)tabs
174198
image:[UIImage systemImageNamed:RCTNSStringFromString(tab.systemImage)]
175199
tag:0];
176200
viewController.tabBarItem.accessibilityIdentifier = RCTNSStringFromString(tab.testID);
177-
viewController.safeAreaInsetsDidChange = ^(UIEdgeInsets insets) {
178-
[weakSelf tabSafeAreaInsetsDidChange:insets];
201+
viewController.safeAreaInsetsDidChange = ^{
202+
[weakSelf updateContentInsets];
179203
};
180204
[viewControllers addObject:viewController];
181205
}
@@ -217,15 +241,19 @@ - (void)updateEventEmitter:(const EventEmitter::Shared &)eventEmitter
217241
{
218242
[super updateEventEmitter:eventEmitter];
219243
// The insets can settle before the first event emitter arrives.
220-
UIViewController *selectedViewController = _tabBarController.selectedViewController;
221-
if (selectedViewController.isViewLoaded && selectedViewController.view.window != nil) {
222-
_contentInsets = UIEdgeInsetsZero;
223-
[self tabSafeAreaInsetsDidChange:selectedViewController.view.safeAreaInsets];
224-
}
244+
_contentInsets = UIEdgeInsetsZero;
245+
[self updateContentInsets];
225246
}
226247
227-
- (void)tabSafeAreaInsetsDidChange:(UIEdgeInsets)insets
248+
// Runs when the selected tab's safe area changes and after each layout pass of the tab bar controller. The
249+
// safe area can change before the pass that moves the tab bar, and the insets depend on where the bar is.
250+
- (void)updateContentInsets
228251
{
252+
UIViewController *selectedViewController = _tabBarController.selectedViewController;
253+
if (!selectedViewController.isViewLoaded || selectedViewController.view.window == nil) {
254+
return;
255+
}
256+
UIEdgeInsets insets = selectedViewController.view.safeAreaInsets;
229257
CGRect tabBarFrame = _tabBarController.tabBar.frame;
230258
CGRect bounds = _tabBarController.view.bounds;
231259
BOOL hasBottomTabBar = !_tabBarController.tabBar.hidden && CGRectGetWidth(tabBarFrame) >= CGRectGetWidth(bounds) &&

0 commit comments

Comments
 (0)