From a532b00e93c03f4f619b223dd618205adfd19a99 Mon Sep 17 00:00:00 2001 From: ArekAras Date: Mon, 9 Nov 2026 13:21:15 +0100 Subject: [PATCH 1/2] Present anchored action sheets from their anchor on iOS 26 iPhones On iOS 26 an iPhone action sheet without a source view is shown centred like an alert, and the anchor option of ActionSheetIOS.showActionSheetWithOptions is ignored on iPhone since 82d2352b19 because an anchored popover lets the background receive touches. Use the anchor on iOS 26 iPhones when one is passed, and cover the window with a transparent control while the anchored sheet is shown, so a tap outside dismisses the sheet (selecting the cancel button) instead of pressing the view under it. An anchor that is no longer in a window falls back to the unanchored presentation. --- .../CoreModules/RCTActionSheetManager.mm | 82 ++++++++++++++++--- 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTActionSheetManager.mm b/packages/react-native/React/CoreModules/RCTActionSheetManager.mm index d615eca6e8a3..988a57da30bc 100644 --- a/packages/react-native/React/CoreModules/RCTActionSheetManager.mm +++ b/packages/react-native/React/CoreModules/RCTActionSheetManager.mm @@ -25,6 +25,8 @@ @interface RCTActionSheetManager () @property (nonatomic, strong) NSMutableArray *alertControllers; +@property (nonatomic, strong) UIControl *outsideTouchShield; +@property (nonatomic, copy) dispatch_block_t outsideTouchHandler; @end @@ -47,27 +49,67 @@ + (BOOL)requiresMainQueueSetup @synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED; +// The anchor view, if it is still on screen. The press that opened the sheet may have removed it. +- (UIView *)anchorViewForTag:(NSNumber *)anchorViewTag +{ + UIView *anchorView = anchorViewTag != nullptr ? [self.viewRegistry_DEPRECATED viewForReactTag:anchorViewTag] : nil; + return anchorView.window != nil ? anchorView : nil; +} + +// On iOS 26 a popover lets the touch that dismisses it reach the views below, so a tap outside +// the sheet would also press whatever React Native view is under the finger. +// A transparent control under the popover takes that touch and dismisses the sheet instead. +- (void)showOutsideTouchShieldInWindow:(UIWindow *)window handler:(dispatch_block_t)handler +{ + [self hideOutsideTouchShield]; + UIControl *shield = [[UIControl alloc] initWithFrame:window.bounds]; + shield.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + [shield addTarget:self action:@selector(outsideTouchShieldTouched) forControlEvents:UIControlEventTouchDown]; + [window addSubview:shield]; + self.outsideTouchShield = shield; + self.outsideTouchHandler = handler; +} + +- (void)outsideTouchShieldTouched +{ + dispatch_block_t handler = self.outsideTouchHandler; + if (handler != nil) { + handler(); + } +} + +- (void)hideOutsideTouchShield +{ + [self.outsideTouchShield removeFromSuperview]; + self.outsideTouchShield = nil; + self.outsideTouchHandler = nil; +} + - (void)presentViewController:(UIViewController *)alertController onParentViewController:(UIViewController *)parentViewController anchorViewTag:(NSNumber *)anchorViewTag { alertController.modalPresentationStyle = UIModalPresentationPopover; UIView *sourceView = parentViewController.view; + UIView *anchorView = [self anchorViewForTag:anchorViewTag]; - if (anchorViewTag != nullptr) { - sourceView = [self.viewRegistry_DEPRECATED viewForReactTag:anchorViewTag]; + if (anchorView != nil) { + sourceView = anchorView; } else { alertController.popoverPresentationController.permittedArrowDirections = 0; } - if ([UIDevice.currentDevice userInterfaceIdiom] == UIUserInterfaceIdiomPad) { - // These information only make sense for iPad, where the action sheet needs - // to be presented from a specific anchor point. - // Before iOS 26, if these information were passed on an iOS app, the action sheet - // was ignoring them. after iOS 26, they are took into consideration and the - // sheet behavior changes, for example the user can interact with the background - // By applying these informations only to the iPad use case, we revert to the previous - // behavior. + // On iOS 26 an iPhone action sheet without a source is shown centred, like an alert. + // When the caller passes an anchor, present the sheet from it as UIKit does in native apps. + BOOL presentsFromAnchor = NO; + if (@available(iOS 26.0, *)) { + presentsFromAnchor = anchorView != nil; + } + + if (presentsFromAnchor || [UIDevice.currentDevice userInterfaceIdiom] == UIUserInterfaceIdiomPad) { + // Before iOS 26, iPhones ignored the source view and rect. On iOS 26 they are used and the popover + // lets the background receive touches, so they are set on iPhone only when an anchor is passed, + // and the outside touch shield blocks the background while the sheet is shown. alertController.popoverPresentationController.sourceView = sourceView; alertController.popoverPresentationController.sourceRect = sourceView.bounds; } @@ -165,6 +207,7 @@ - (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowAction if (!callbackInvoked) { callbackInvoked = true; [self->_alertControllers removeObject:alertController]; + [self hideOutsideTouchShield]; callback(@[ @(localIndex) ]); } }]; @@ -212,6 +255,24 @@ - (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowAction } [self->_alertControllers addObject:alertController]; + + UIView *anchorView = nil; + if (@available(iOS 26.0, *)) { + anchorView = [self anchorViewForTag:anchorViewTag]; + } + if (anchorView != nil) { + [self showOutsideTouchShieldInWindow:anchorView.window + handler:^{ + [alertController dismissViewControllerAnimated:YES completion:nil]; + [self->_alertControllers removeObject:alertController]; + [self hideOutsideTouchShield]; + // As in UIKit, a tap outside the popover selects the cancel button, if any. + if (!callbackInvoked && cancelButtonIndex >= 0) { + callbackInvoked = true; + callback(@[ @(cancelButtonIndex) ]); + } + }]; + } [self presentViewController:alertController onParentViewController:controller anchorViewTag:anchorViewTag]; }); } @@ -226,6 +287,7 @@ - (void)dismissActionSheet dispatch_async(dispatch_get_main_queue(), ^{ [alertController dismissViewControllerAnimated:YES completion:nil]; [self->_alertControllers removeLastObject]; + [self hideOutsideTouchShield]; }); } From b1c821901a2d3882465e8747168e14941feac10a Mon Sep 17 00:00:00 2001 From: ArekAras Date: Fri, 20 Nov 2026 17:54:29 +0100 Subject: [PATCH 2/2] Address review: weak self, shield for share sheet, iPhone only - Break the self -> handler -> self cycle with a weak reference. - Remove an old shield before presenting, and add the shield in the same place for both sheets. - Anchored share sheets get the shield too; an outside tap reports completed: false once. - Keep the shield while the share sheet stays open after backing out of an activity. - Add the shield to the presenting window, iPhone only; iPad keeps UIKit's popover behaviour. - Hide the shield from accessibility. --- .../CoreModules/RCTActionSheetManager.mm | 70 ++++++++++++++----- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTActionSheetManager.mm b/packages/react-native/React/CoreModules/RCTActionSheetManager.mm index 988a57da30bc..15da8514b1e5 100644 --- a/packages/react-native/React/CoreModules/RCTActionSheetManager.mm +++ b/packages/react-native/React/CoreModules/RCTActionSheetManager.mm @@ -63,6 +63,7 @@ - (void)showOutsideTouchShieldInWindow:(UIWindow *)window handler:(dispatch_bloc { [self hideOutsideTouchShield]; UIControl *shield = [[UIControl alloc] initWithFrame:window.bounds]; + shield.isAccessibilityElement = NO; shield.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [shield addTarget:self action:@selector(outsideTouchShieldTouched) forControlEvents:UIControlEventTouchDown]; [window addSubview:shield]; @@ -88,7 +89,9 @@ - (void)hideOutsideTouchShield - (void)presentViewController:(UIViewController *)alertController onParentViewController:(UIViewController *)parentViewController anchorViewTag:(NSNumber *)anchorViewTag + outsideTouchHandler:(dispatch_block_t)outsideTouchHandler { + [self hideOutsideTouchShield]; alertController.modalPresentationStyle = UIModalPresentationPopover; UIView *sourceView = parentViewController.view; UIView *anchorView = [self anchorViewForTag:anchorViewTag]; @@ -113,6 +116,10 @@ - (void)presentViewController:(UIViewController *)alertController alertController.popoverPresentationController.sourceView = sourceView; alertController.popoverPresentationController.sourceRect = sourceView.bounds; } + // iPad keeps its popover behaviour, so the shield is only added on iPhone. + if (presentsFromAnchor && [UIDevice.currentDevice userInterfaceIdiom] != UIUserInterfaceIdiomPad) { + [self showOutsideTouchShieldInWindow:parentViewController.view.window handler:outsideTouchHandler]; + } [parentViewController presentViewController:alertController animated:YES completion:nil]; } @@ -256,24 +263,24 @@ - (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowAction [self->_alertControllers addObject:alertController]; - UIView *anchorView = nil; - if (@available(iOS 26.0, *)) { - anchorView = [self anchorViewForTag:anchorViewTag]; - } - if (anchorView != nil) { - [self showOutsideTouchShieldInWindow:anchorView.window - handler:^{ - [alertController dismissViewControllerAnimated:YES completion:nil]; - [self->_alertControllers removeObject:alertController]; - [self hideOutsideTouchShield]; - // As in UIKit, a tap outside the popover selects the cancel button, if any. - if (!callbackInvoked && cancelButtonIndex >= 0) { - callbackInvoked = true; - callback(@[ @(cancelButtonIndex) ]); - } - }]; - } - [self presentViewController:alertController onParentViewController:controller anchorViewTag:anchorViewTag]; + __weak RCTActionSheetManager *weakSelf = self; + [self presentViewController:alertController + onParentViewController:controller + anchorViewTag:anchorViewTag + outsideTouchHandler:^{ + RCTActionSheetManager *strongSelf = weakSelf; + if (strongSelf == nil) { + return; + } + [alertController dismissViewControllerAnimated:YES completion:nil]; + [strongSelf->_alertControllers removeObject:alertController]; + [strongSelf hideOutsideTouchShield]; + // As in UIKit, a tap outside the popover selects the cancel button, if any. + if (!callbackInvoked && cancelButtonIndex >= 0) { + callbackInvoked = true; + callback(@[ @(cancelButtonIndex) ]); + } + }]; }); } @@ -345,8 +352,30 @@ - (void)showShareActionSheetWithOptions: } UIViewController *controller = RCTPresentedViewController(); + __weak RCTActionSheetManager *weakSelf = self; + __weak UIActivityViewController *weakShareController = shareController; + __block bool callbackInvoked = false; + dispatch_block_t outsideTouchHandler = [^{ + [weakSelf hideOutsideTouchShield]; + [weakShareController dismissViewControllerAnimated:YES completion:nil]; + // Same result as closing the share sheet: not completed, no activity. + if (!callbackInvoked) { + callbackInvoked = true; + successCallback(@[ @NO, [NSNull null] ]); + } + } copy]; shareController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) { + // Backing out of an activity calls this with the share sheet still open, so the shield stays. + // A later sheet may have its own shield by now, so only this sheet's shield is removed. + RCTActionSheetManager *strongSelf = weakSelf; + if ((activityError != nullptr || completed || activityType == nil) && + strongSelf.outsideTouchHandler == outsideTouchHandler) { + [strongSelf hideOutsideTouchShield]; + } + if (callbackInvoked) { + return; + } if (activityError != nullptr) { failureCallback(@[ RCTJSErrorFromNSError(activityError) ]); } else if (completed || activityType == nil) { @@ -364,7 +393,10 @@ - (void)showShareActionSheetWithOptions: shareController.overrideUserInterfaceStyle = UIUserInterfaceStyleLight; } - [self presentViewController:shareController onParentViewController:controller anchorViewTag:anchorViewTag]; + [self presentViewController:shareController + onParentViewController:controller + anchorViewTag:anchorViewTag + outsideTouchHandler:outsideTouchHandler]; }); }