Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +60,7 @@ - (void)viewSafeAreaInsetsDidChange
{
[super viewSafeAreaInsetsDidChange];
if (self.safeAreaInsetsDidChange != nil) {
self.safeAreaInsetsDidChange(self.view.safeAreaInsets);
self.safeAreaInsetsDidChange();
}
}

Expand All @@ -50,7 +70,7 @@ @interface RNTesterTabsIOSComponentView () <UITabBarControllerDelegate>
@end

@implementation RNTesterTabsIOSComponentView {
UITabBarController *_tabBarController;
RNTesterTabBarController *_tabBarController;
// Holds the mounted React children, and moves into the selected tab.
UIView *_containerView;
NSArray<NSString *> *_tabKeys;
Expand All @@ -77,25 +97,40 @@ - (instancetype)initWithFrame:(CGRect)frame
_containerView = [UIView new];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

_tabBarController = [UITabBarController new];
__weak RNTesterTabsIOSComponentView *weakSelf = self;
_tabBarController = [RNTesterTabBarController new];
_tabBarController.delegate = self;
self.contentView = _tabBarController.view;
_tabBarController.didLayoutSubviews = ^{
[weakSelf updateContentInsets];
};
}
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];
}
}
Expand Down Expand Up @@ -163,8 +198,8 @@ - (void)createTabs:(const std::vector<RNTesterTabsIOSTabsStruct> &)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];
}
Expand Down Expand Up @@ -206,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) &&
Expand Down
Loading