-
Notifications
You must be signed in to change notification settings - Fork 25.3k
[RNTester][iOS] Add tab bar controller, minimally support iPhone Duo #58664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huntie
wants to merge
1
commit into
react:main
Choose a base branch
from
huntie:rntester-native-navigator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/rn-tester/RNTesterTabsIOS/RNTesterTabsIOS.podspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the MIT license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| require "json" | ||
|
|
||
| package = JSON.parse(File.read(File.join(__dir__, "../" "package.json"))) | ||
|
|
||
| Pod::Spec.new do |s| | ||
| s.name = "RNTesterTabsIOS" | ||
| s.version = package["version"] | ||
| s.summary = package["description"] | ||
| s.description = "RNTester's native tab bar" | ||
| s.homepage = "https://github.com/react/react-native" | ||
| s.license = "MIT" | ||
| s.platforms = min_supported_versions | ||
| s.author = "Meta Platforms, Inc. and its affiliates" | ||
| s.source = { :git => "https://github.com/react/react-native.git", :tag => "#{s.version}" } | ||
| s.pod_target_xcconfig = { | ||
| "HEADER_SEARCH_PATHS" => "\"${PODS_CONFIGURATION_BUILD_DIR}/ReactCodegen/ReactCodegen.framework/Headers\"", | ||
| "CLANG_CXX_LANGUAGE_STANDARD" => rct_cxx_language_standard() | ||
| } | ||
|
|
||
| s.source_files = "ios/**/*.{h,mm}" | ||
| s.requires_arc = true | ||
|
|
||
| install_modules_dependencies(s) | ||
| end |
23 changes: 23 additions & 0 deletions
23
packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import <React/RCTViewComponentView.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /** | ||
| * Hosts RNTester's tabs in a UITabBarController, so the tab bar adapts to the | ||
| * device (a bottom bar, or a sidebar on wide layouts) while the app stays a | ||
| * single React Native root. | ||
| * | ||
| * The React children are the selected tab's content: they are mounted into a | ||
| * single container view, which moves into the selected tab's view controller. | ||
| */ | ||
| @interface RNTesterTabsIOSComponentView : RCTViewComponentView | ||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
228 changes: 228 additions & 0 deletions
228
packages/rn-tester/RNTesterTabsIOS/ios/RNTesterTabsIOSComponentView.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import "RNTesterTabsIOSComponentView.h" | ||
|
|
||
| #import <React/RCTConversions.h> | ||
| #import <React/UIView+React.h> | ||
| #import <react/renderer/components/AppSpecs/ComponentDescriptors.h> | ||
| #import <react/renderer/components/AppSpecs/EventEmitters.h> | ||
| #import <react/renderer/components/AppSpecs/Props.h> | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| using namespace facebook::react; | ||
|
|
||
| /** | ||
| * 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); | ||
| @end | ||
|
|
||
| @implementation RNTesterTabViewController | ||
|
|
||
| - (void)viewSafeAreaInsetsDidChange | ||
| { | ||
| [super viewSafeAreaInsetsDidChange]; | ||
| if (self.safeAreaInsetsDidChange != nil) { | ||
| self.safeAreaInsetsDidChange(self.view.safeAreaInsets); | ||
| } | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| @interface RNTesterTabsIOSComponentView () <UITabBarControllerDelegate> | ||
| @end | ||
|
|
||
| @implementation RNTesterTabsIOSComponentView { | ||
| UITabBarController *_tabBarController; | ||
| // Holds the mounted React children, and moves into the selected tab. | ||
| UIView *_containerView; | ||
| std::vector<std::string> _tabKeys; | ||
| UIEdgeInsets _contentInsets; | ||
| } | ||
|
|
||
| + (ComponentDescriptorProvider)componentDescriptorProvider | ||
| { | ||
| return concreteComponentDescriptorProvider<RNTesterTabsIOSComponentDescriptor>(); | ||
| } | ||
|
|
||
| // The view owns a view controller, which is not worth resetting for reuse. | ||
| + (BOOL)shouldBeRecycled | ||
| { | ||
| return NO; | ||
| } | ||
|
|
||
| - (instancetype)initWithFrame:(CGRect)frame | ||
| { | ||
| if (self = [super initWithFrame:frame]) { | ||
| static const auto defaultProps = std::make_shared<const RNTesterTabsIOSProps>(); | ||
| _props = defaultProps; | ||
|
|
||
| _containerView = [UIView new]; | ||
| _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | ||
|
|
||
| _tabBarController = [UITabBarController new]; | ||
| _tabBarController.delegate = self; | ||
| self.contentView = _tabBarController.view; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)didMoveToWindow | ||
| { | ||
| [super didMoveToWindow]; | ||
| if (self.window == nil) { | ||
| [_tabBarController willMoveToParentViewController:nil]; | ||
| [_tabBarController removeFromParentViewController]; | ||
| return; | ||
| } | ||
|
|
||
| UIViewController *parentViewController = self.reactViewController; | ||
| if (parentViewController != nil && _tabBarController.parentViewController == nil) { | ||
| [parentViewController addChildViewController:_tabBarController]; | ||
| [_tabBarController didMoveToParentViewController:parentViewController]; | ||
| } | ||
| } | ||
|
|
||
| #pragma mark - Children | ||
|
|
||
| // Children are laid out against this view's bounds, which the tab's view shares, so they keep their | ||
| // frames wherever the container moves. | ||
| - (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index | ||
| { | ||
| [_containerView insertSubview:childComponentView atIndex:index]; | ||
| } | ||
|
|
||
| - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index | ||
| { | ||
| [childComponentView removeFromSuperview]; | ||
| } | ||
|
|
||
| #pragma mark - Props | ||
|
|
||
| - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps | ||
| { | ||
| const auto &oldTabsProps = static_cast<const RNTesterTabsIOSProps &>(*_props); | ||
| const auto &newTabsProps = static_cast<const RNTesterTabsIOSProps &>(*props); | ||
|
|
||
| // RNTester's tabs are a constant, so they are built once, from the first props. | ||
| bool tabsCreated = _tabKeys.empty(); | ||
| if (tabsCreated) { | ||
| [self createTabs:newTabsProps.tabs]; | ||
| } | ||
|
Comment on lines
+116
to
+119
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error if tabKeys are not equal? |
||
| if (tabsCreated || oldTabsProps.selectedTab != newTabsProps.selectedTab) { | ||
| [self selectTab:newTabsProps.selectedTab]; | ||
| } | ||
| if (oldTabsProps.tabBarHidden != newTabsProps.tabBarHidden) { | ||
| if (@available(iOS 18.0, tvOS 18.0, *)) { | ||
| [_tabBarController setTabBarHidden:newTabsProps.tabBarHidden animated:NO]; | ||
| } else { | ||
| _tabBarController.tabBar.hidden = newTabsProps.tabBarHidden; | ||
| } | ||
| } | ||
|
|
||
| [super updateProps:props oldProps:oldProps]; | ||
| } | ||
|
|
||
| - (void)createTabs:(const std::vector<RNTesterTabsIOSTabsStruct> &)tabs | ||
| { | ||
| __weak RNTesterTabsIOSComponentView *weakSelf = self; | ||
| NSMutableArray<UIViewController *> *viewControllers = [NSMutableArray arrayWithCapacity:tabs.size()]; | ||
| _tabKeys.clear(); | ||
|
|
||
| for (const auto &tab : tabs) { | ||
| auto *viewController = [RNTesterTabViewController new]; | ||
| viewController.tabBarItem = | ||
| [[UITabBarItem alloc] initWithTitle:RCTNSStringFromString(tab.title) | ||
| image:[UIImage systemImageNamed:RCTNSStringFromString(tab.systemImage)] | ||
| tag:0]; | ||
| viewController.tabBarItem.accessibilityIdentifier = RCTNSStringFromString(tab.testID); | ||
| viewController.safeAreaInsetsDidChange = ^(UIEdgeInsets insets) { | ||
| [weakSelf tabSafeAreaInsetsDidChange:insets]; | ||
| }; | ||
| [viewControllers addObject:viewController]; | ||
| _tabKeys.push_back(tab.key); | ||
| } | ||
|
|
||
| _tabBarController.viewControllers = viewControllers; | ||
| } | ||
|
|
||
| - (void)selectTab:(const std::string &)key | ||
| { | ||
| auto it = std::find(_tabKeys.begin(), _tabKeys.end(), key); | ||
| if (it == _tabKeys.end()) { | ||
| return; | ||
| } | ||
| _tabBarController.selectedIndex = std::distance(_tabKeys.begin(), it); | ||
|
|
||
| UIView *tabView = _tabBarController.selectedViewController.view; | ||
| _containerView.frame = tabView.bounds; | ||
| [tabView addSubview:_containerView]; | ||
| } | ||
|
|
||
| #pragma mark - UITabBarControllerDelegate | ||
|
|
||
| - (BOOL)tabBarController:(UITabBarController *)tabBarController | ||
| shouldSelectViewController:(UIViewController *)viewController | ||
| { | ||
| NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController]; | ||
| if (index != NSNotFound && _eventEmitter != nullptr) { | ||
| static_cast<const RNTesterTabsIOSEventEmitter &>(*_eventEmitter).onTabPress({.key = _tabKeys[index]}); | ||
| } | ||
| // JS selects the tab by updating `selectedTab`. | ||
| return NO; | ||
| } | ||
|
|
||
| #pragma mark - Insets | ||
|
|
||
| - (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]; | ||
| } | ||
| } | ||
|
|
||
| - (void)tabSafeAreaInsetsDidChange:(UIEdgeInsets)insets | ||
| { | ||
| // The home indicator's inset only applies beneath a tab bar along the bottom edge, which already | ||
| // clears it. With the tab bar at the side, content runs to the bottom edge. | ||
| CGRect tabBarFrame = _tabBarController.tabBar.frame; | ||
| CGRect bounds = _tabBarController.view.bounds; | ||
| if (CGRectGetWidth(tabBarFrame) < CGRectGetWidth(bounds) || CGRectGetMaxY(tabBarFrame) < CGRectGetMaxY(bounds)) { | ||
| insets.bottom = 0; | ||
| } | ||
| // With no status bar above it, UINavigationBar still sits 24pt below the top edge, clear of the | ||
| // display's corners. Content leaves the same gap, since the device reports no top inset there. | ||
| if (insets.top == 0) { | ||
| insets.top = 24; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems weird to have this hard-coded |
||
| } | ||
| if (UIEdgeInsetsEqualToEdgeInsets(insets, _contentInsets) || _eventEmitter == nullptr) { | ||
| return; | ||
| } | ||
| _contentInsets = insets; | ||
| static_cast<const RNTesterTabsIOSEventEmitter &>(*_eventEmitter) | ||
| .onContentInsetsChange({ | ||
| .top = insets.top, | ||
| .left = insets.left, | ||
| .bottom = insets.bottom, | ||
| .right = insets.right, | ||
| }); | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| Class<RCTComponentViewProtocol> RNTesterTabsIOSCls(void) | ||
| { | ||
| return RNTesterTabsIOSComponentView.class; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use C++ here instead of
NSArray<NSString *> *>?