react native navigator禁用滑动返回

    xiaoxiao2021-11-30  44

    转载:http://blog.csdn.net/pz789as/article/details/52606635

    在React-Native开发中,经常会用到导航。导航做什么用的呢,简单点说就是页面跳转。

    一个项目中,肯定有很多的页面要跳来跳去的,RN就给我们提供了Navigator组件,可以很好的管理页面的跳转。

    在所有工作做完之后,发现有个bug!在从第一个界面跳转到下一个界面后,如果从屏幕左边向右滑,或者从上面想下滑,你会发现一个神奇的事情,那就是页面会通过滑动而返回到上一个界面。这让我们很尴尬了,本来打算禁止跳转返回的,或者返回时还要做些什么处理的,结果啥都没做,直接返回,可以说,这个功能有点适得其反了。

    于是为了解决这个问题,到处找答案,官网没有说,论坛也没人回答。于是放置了很久很久,没想到在今天的而然查找下,终于找到解决方案了。

    方案主要分三种:

    1,自己定义个configureScene:

    [html]  view plain  copy const NoBackSwipe = {     ...Navigator.SceneConfigs.HorizontalSwipeJump,       gestures: {         pop: {}       }   };   然后在Navigator标签下使用

    [html]  view plain  copy <Navigator          initialRoute={{Component:'xxx', name:'xxx', index:0, configure: NoBackSwipe}}         renderScene={this.renderScene.bind(this)}         configureScene={(route,routeStack)=>{           return NoBackSwipe         }}       />   这里主要是处理了pop,其中还有jumpback,jumpforward的

    2,如果你都不要返回处理的,直接将gestures都改成{}或者null

    [html]  view plain  copy configureScene(route, routeStack){     let configure = Navigator.SceneConfigs.PushFromRight;     switch(route.configure){       case Consts.FloatFromLeft:         configure = Navigator.SceneConfigs.FloatFromLeft;         break;       case Consts.FloatFromBottom:         configure = Navigator.SceneConfigs.FloatFromBottom;         break;     }     return {       ...configure,       gestures:{}//或者改成null     };   }   然后使用也是一样:

    [html]  view plain  copy <Navigator initialRoute={{Component:'xxx', name:'xxx', index:0, configure: NoBackSwipe}}           configureScene={this.configureScene.bind(this)}           renderScene={this.renderScene.bind(this)}           onStartShouldSetResponder={()=>false}/>   我就是用的第二种。

    3,还有一种,是直接改源码,在项目目录下找到路径:

    /node_modules/react-native/Libraries/CustomComponents/Navigator/Navigator.js

    里面有一段代码,去掉pop就可以了

    [html]  view plain  copy var GESTURE_ACTIONS = [     'pop',//把这个去掉就可以了     'jumpBack',     'jumpForward',   ];   这种直接修改源码的不推荐使用,因为每当你要升级RN或者做其他调整时,重新下载下来又得改,还是上面两种比较靠谱。

    转载请注明原文地址: https://ju.6miu.com/read-679231.html

    最新回复(0)