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

fix(iOS): app freeze when navigating back from any modal nested in contained modal #1996

Merged
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Example/src/screens/Modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type StackParamList = {
Modal: undefined;
FullscreenModal: undefined;
Alert: undefined;
ContainedModal: undefined;
};

interface MainScreenProps {
Expand All @@ -25,6 +26,10 @@ const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => (
onPress={() => navigation.navigate('FullscreenModal')}
/>
<Button title="Open alert" onPress={() => navigation.navigate('Alert')} />
<Button
title="Open contained modal"
onPress={() => navigation.navigate('ContainedModal')}
/>
<Button onPress={() => navigation.pop()} title="🔙 Back to Examples" />
</View>
);
Expand All @@ -41,6 +46,10 @@ const ModalScreen = ({ navigation }: ModalScreenProps): JSX.Element => (
onPress={() => navigation.push('FullscreenModal')}
/>
<Button title="Open alert" onPress={() => navigation.navigate('Alert')} />
<Button
title="Open contained modal"
onPress={() => navigation.navigate('ContainedModal')}
/>
<Button title="Go back" onPress={() => navigation.goBack()} />
</View>
);
Expand Down Expand Up @@ -68,6 +77,11 @@ const App = (): JSX.Element => (
component={ModalScreen}
options={{ stackPresentation: 'fullScreenModal' }}
/>
<Stack.Screen
name="ContainedModal"
component={ModalScreen}
options={{ stackPresentation: 'containedModal' }}
/>
<Stack.Screen
name="Alert"
component={Alert}
Expand Down
2 changes: 1 addition & 1 deletion TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ import Test1683 from './src/Test1683';
import Test1726 from './src/Test1726';
import Test1791 from './src/Test1791';
import Test1802 from './src/Test1802';
import Test1829 from './src/Test1829';
import Test1844 from './src/Test1844';
import Test1864 from './src/Test1864';
import Test1829 from './src/Test1829';
import Test1981 from './src/Test1981';

enableFreeze(true);
Expand Down
16 changes: 14 additions & 2 deletions ios/RNSScreenStack.mm
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,27 @@ - (void)setModalViewControllers:(NSArray<UIViewController *> *)controllers
}
}

// changeRootController does not have presentedViewController but it does not mean that no modals are in presentation,
// so we need to find top-level controller manually
// changeRootController does not have presentedViewController but it does not mean that no modals are in presentation;
// modals could be presented by another stack (nested / outer), third-party view controller or they could be using
// UIModalPresentationCurrentContext / UIModalPresentationOverCurrentContext presentation styles; in the last case
// for some reason system asks top-level (react root) vc to present instead of our stack, despite the fact that
// `definesPresentationContext` returns `YES` for UINavigationController.
// So we first need to find top-level controller manually:
UIViewController *reactRootVc = [self findReactRootViewController];
UIViewController *topMostVc = [RNSScreenStackView findTopMostPresentedViewControllerFromViewController:reactRootVc];

if (topMostVc != reactRootVc) {
changeRootController = topMostVc;

// Here we handle just the simplest case where the top level VC was dismissed. In any more complex
// scenario we will still have problems, see: https://github.com/software-mansion/react-native-screens/issues/1813
if ([_presentedModals containsObject:topMostVc] && ![controllers containsObject:topMostVc]) {
[changeRootController dismissViewControllerAnimated:YES completion:finish];
return;
}
}

// We didn't detect any controllers for dismissal, thus we start presenting new VCs
finish();
}

Expand Down
Loading