I'm using the core React Native Modal component. Within the modal content I have a Done
button.
Pressing Done
is the only way we want users to close the modal. But the Modal component allows swiping down from the top of the screen to close.
How do you turn off "swipe to close"?
Disable the swipe gesture for the screen ( gestureEnabled: false ). Override the native back button in the header with a custom back button ( headerLeft: (props) => <CustomBackButton {... props} /> ).
In order to display something when we swipe either to the right or to the left, we have to render a component inside the props: renderLeftActions and/or renderRightActions . And to trigger a function on swipe we have to pass a function definition to onSwipeableLeftOpen and/or onSwipeableRightOpen .
To answer @Nikolai in the comments, I am using React Navigation.
I didn't realize the gesture settings from the navigator also controls the gestures of the react native modal.
Turning off gestures solved my problem.
const HomeScreenContainer = StackNavigator(
{
HomeScreen: { screen: Screens.HomeScreen },
PostScreen: { screen: Screens.PostScreen },
CameraScreen: { screen: Screens.CameraScreen },
CameraRollScreen: { screen: Screens.CameraRollScreen },
},
{
navigationOptions: {
gesturesEnabled: false,
},
},
);
Struggled with it a bit too. Here is what worked for me:
If you have root navigator as modal and inside it another stacked navigator for which you want to disable gestures, then put this inside root navigator for the stacked navigator, worked for me in v2.12 iOS
navigationOptions: {
gesturesEnabled: false,
},
here's full code:
const RootStack = createStackNavigator(
{
LoginNavigator: {
screen: LoginNavigator,
navigationOptions: {
gesturesEnabled: false,
},
},
ModerationNavigator: {
screen: ModerationNavigator,
},
WalletNavigator: {
screen: WalletNavigator,
},
FloatingNavigator: {
screen: FloatingNavigator,
},
UIKitNavigator: {
screen: UIKitNavigator,
},
MainMapViewScreen: {
screen: MainMapViewScreen,
},
FullscreenPhotoScreen: {
screen: FullscreenPhotoScreen,
},
},
{
mode: 'modal',
initialRouteName: 'MainMapViewScreen',
headerMode: 'none',
header: null,
},
);
Since React Navigation Version 5.x, they have changed it to gestureEnabled
instead of gesturesEnabled
(without the 's) for both StackNavigator and DrawerNavigator
Sample usage:
<Stack.Navigator mode="modal" screenOptions={{ gestureEnabled: false }}>
<Stack.Screen name="HomeNav" component={HomeNavigator} />
</Stack.Navigator>
In addition to @GollyJer's answer, if you want to disable the swipe gesture for a single Modal you can also do this:
const AppNavigator = StackNavigator({
ModalScreen: {
screen: ModalScreen,
navigationOptions: {
gesturesEnabled: false
},
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With