简体   繁体   中英

Is there a way to conditionally disable the swipe to go back gesture in react native iOS?

I'm looking for a way to conditionally disable the swipe to go back gesture in react native iOS. I'm using the react-navigation library to control navigation. For Android, I am able to do it using BackHandler. Is it possible to do something similar in iOS?

  componentDidMount() {
      BackHandler.addEventListener("hardwareBackPress", this.handleBackButton);
  }

  handleBackButton = () => {
    if (this.props.creating) {
      return true; // Disables the back button in Android
    }
  };

  componentWillUnmount() {
      BackHandler.removeEventListener(
        "hardwareBackPress",
        this.handleBackButton
      );
  }

Yes, there is an option to disable gestures:

screen: ExampleView
    navigationOptions: {
        gesturesEnabled: false,
    },

Check for more details here: https://github.com/react-navigation/react-navigation/issues/1063

But I'm trying to only disable gestures when this.props.creating is true.

Try adding this static method to your component.

   static navigationOptions = props => {
      return {
        gesturesEnabled: this.props.creating ? false : true
      }
    };

StackNavigator takes navigationOptions representing the "Default navigation options to use for screens"

Example:

 const RootStackNavigator = StackNavigator(
  {
    Login: {
      screen: LoginScreen
    },
    Main: {
      screen: MainScreen
    }
  },
  {
    initialRouteName: 'Login',
    navigationOptions: {
      gesturesEnabled: false   // <- add this line
    }
  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM