【问题标题】:Is there a way to conditionally disable the swipe to go back gesture in react native iOS?有没有办法在反应原生 iOS 中有条件地禁用滑动返回手势?
【发布时间】:2019-10-22 11:45:39
【问题描述】:

我正在寻找一种方法来有条件地禁用滑动返回手势以响应原生 iOS。我正在使用react-navigation 库来控制导航。对于 Android,我可以使用 BackHandler 来完成。是否可以在 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
      );
  }

【问题讨论】:

  • 你试过我的答案了吗?

标签: ios react-native react-navigation


【解决方案1】:

是的,有一个禁用手势的选项:

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

在此处查看更多详细信息:https://github.com/react-navigation/react-navigation/issues/1063

【讨论】:

  • 这将完全禁用屏幕手势。但我试图仅在 this.props.creating 为真时禁用手势。
  • 如果您依赖prop,我会假设它来自上层范围。可以在Navigator/Screen 级别上禁用有条件的回扫
【解决方案2】:

但我试图仅在 this.props.creating 为 真的。

尝试将此静态方法添加到您的组件中。

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

【讨论】:

    【解决方案3】:

    StackNavigator takes navigationOptions 代表“用于屏幕的默认导航选项”

    例子:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2022-12-29
      • 2016-08-25
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      相关资源
      最近更新 更多