diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm index 350439a78109dc..a040149dfa656c 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm @@ -134,7 +134,9 @@ - (void)presentViewController:(UIViewController *)modalViewController completion:(void (^)(void))completion { UIViewController *controller = [self reactViewController]; - [controller presentViewController:modalViewController animated:animated completion:completion]; + [[self _topMostViewControllerFrom:controller] presentViewController:modalViewController + animated:animated + completion:completion]; } - (void)dismissViewController:(UIViewController *)modalViewController @@ -274,6 +276,26 @@ - (void)unmountChildComponentView:(UIView *)childCompo [childComponentView removeFromSuperview]; } +#pragma mark - Private + +- (UIViewController *)_topMostViewControllerFrom:(UIViewController *)rootViewController +{ + UIViewController *topController = rootViewController; + while (topController.presentedViewController) { + topController = topController.presentedViewController; + } + if ([topController isKindOfClass:[UINavigationController class]]) { + UINavigationController *navigationController = (UINavigationController *)topController; + topController = navigationController.visibleViewController; + return [self _topMostViewControllerFrom:topController]; + } else if ([topController isKindOfClass:[UITabBarController class]]) { + UITabBarController *tabBarController = (UITabBarController *)topController; + topController = tabBarController.selectedViewController; + return [self _topMostViewControllerFrom:topController]; + } + return topController; +} + @end #ifdef __cplusplus