Skip to content
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

feat: prevent native back button dismissal on iOS #1773

Merged
merged 4 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions ios/RNSScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ NS_ASSUME_NONNULL_BEGIN
#endif

- (void)notifyTransitionProgress:(double)progress closing:(BOOL)closing goingForward:(BOOL)goingForward;
- (void)notifyDismissCancelledWithDismissCount:(int)dismissCount;
- (BOOL)isModal;

@end
Expand Down
21 changes: 19 additions & 2 deletions ios/RNSScreenStack.mm
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,11 @@ - (void)dismissOnReload
screen = ((RNSScreen *)fromVC).screenView;
}
if (screen != nil &&
// we need to return the animator when full width swiping even if the animation is not custom,
// when preventing the native dismiss with back button, we have to return the animator.
// Also, we need to return the animator when full width swiping even if the animation is not custom,
// otherwise the screen will be just popped immediately due to no animation
(_isFullWidthSwiping || [RNSScreenStackAnimator isCustomAnimation:screen.stackAnimation])) {
((operation == UINavigationControllerOperationPop && screen.preventNativeDismiss) || _isFullWidthSwiping ||
[RNSScreenStackAnimator isCustomAnimation:screen.stackAnimation])) {
return [[RNSScreenStackAnimator alloc] initWithOperation:operation];
}
return nil;
Expand Down Expand Up @@ -727,6 +729,21 @@ - (void)handleSwipe:(UIPanGestureRecognizer *)gestureRecognizer
interactionControllerForAnimationController:
(id<UIViewControllerAnimatedTransitioning>)animationController
{
RNSScreenView *sourceView = [_controller.transitionCoordinator viewForKey:UITransitionContextFromViewKey];
RNSScreenView *targetView = [_controller.transitionCoordinator viewForKey:UITransitionContextToViewKey];
// we can intercept clicking back button here, we check reactSuperview since this method also fires when
if (_interactionController == nil && sourceView.reactSuperview && sourceView.preventNativeDismiss) {
_interactionController = [UIPercentDrivenInteractiveTransition new];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self->_interactionController cancelInteractiveTransition];
self->_interactionController = nil;
int sourceIndex = (int)[[self reactSubviews] indexOfObject:sourceView];
int targetIndex = (int)[[self reactSubviews] indexOfObject:targetView];
int dismissCount = sourceIndex - targetIndex > 0 ? sourceIndex - targetIndex : 1;
WoLewicki marked this conversation as resolved.
Show resolved Hide resolved
[self updateContainer];
[sourceView notifyDismissCancelledWithDismissCount:dismissCount];
});
}
return _interactionController;
}

Expand Down