如何仅对导航器屏幕之一禁用抽屉式导航滑动?

时间:2019-03-14 20:57:03

标签: javascript react-native react-navigation

我有一个自定义的导航抽屉,并且在特定屏幕上,我不希望它可用。

这是我的短代码。

此导航器也包含在开关导航器中。我在git-hub和其他论坛中进行了挖掘,目前无任何作用。我想念什么吗?有没有人使它起作用?

const UserNavigation = createDrawerNavigator({
    ProductListScreen: {screen: ProductListScreen},
    ProductHistoryScreen: {
      screen: ProductHistoryScreen,
      navigationOptions: {
        drawerLockMode: 'locked-closed'
      }
    }
}, {
    initialRouteName: 'ProductListScreen',
    contentComponent: CustomDrawerContentComponent,
})

export default createAppContainer(UserNavigation)

还有一个working code on expo,但我尝试执行并导致显示两个导航器,并且在屏幕上我不想显示它出现的抽屉。这是我尝试参考博览会代码

const UserStackNavigation = createStackNavigator({
  ProductListScreen: {screen: ProductListScreen},
  ProductHistoryScreen: {
    screen: ProductHistoryScreen
  }
})

const UserNavigation = createDrawerNavigator({
  UserStackNavigation: UserStackNavigation
}, {
  initialRouteName: 'UserStackNavigation',
  contentComponent: CustomDrawerContentComponent,
})

UserStackNavigation.navigationOptions = ({ navigation }) => ({
  drawerLockMode: navigation.state.index === 0 ? 'unlocked' : 'locked-closed',
});
export default createAppContainer(UserNavigation)

5 个答案:

答案 0 :(得分:3)

在v5中,您可以使用<Drawer.Navigator screenOptions={{ gestureEnabled: false }} ...>...</Drawer.Navigator>

答案 1 :(得分:0)

问题是我正在使用Ignite生成我的项目模板,而反应导航版本已固定为3.0.0。在看完this link上的最后一条评论之后,我意识到我的语法没有任何问题。

所以我删除了package.json中的packed.lock.json,yarn.lock,node_modules文件夹,将版本设置为^ 3.0.0,以获得最新版本。

所有这些之后,我进行了纱安装以获取完整的软件包更新。进行此更改后,我的react-navigation-drawer从1.0.1更改为1.3.0,从而解决了该问题。

还对导航进行了一些更改,如下所示:

export default class UserNavigation extends React.Component{
  render(){
    return <Nav />
  }
}

const Nav = createAppContainer(
  createDrawerNavigator(
    {
      ProductListScreen: {screen: ProductListScreen},
      ProductHistoryScreen: {
        screen: ProductHistoryScreen,
          navigationOptions:{
            drawerLockMode: 'locked-closed'
          }
      }
    }, {
      initialRouteName: 'ProductListScreen',
      contentComponent: CustomDrawerContentComponent,
    }
  )
)

答案 2 :(得分:0)

要在React Navigation V5中禁用一条路线中的滑动/手势:

导入助手以获取当前路线名称

import { getFocusedRouteNameFromRoute } from '@react-navigation/native';

检查路线名称是否与您要禁用滑动/手势的路线相同

options={({ route }) => {
   const routeName = getFocusedRouteNameFromRoute(route);
   return {
     swipeEnabled: routeName !== 'Profile',
   };
}}

答案 3 :(得分:0)

您可以使用“ swipeEnabled”属性在每个屏幕上设置选项。请看一下文档的相关页面;

https://reactnavigation.org/docs/drawer-navigator/#options

<Drawer.Navigator initialRouteName="Feed">
  <Drawer.Screen
    name="Feed"
    component={Feed}
    options={{ swipeEnabled: false }}
  />
  <Drawer.Screen
    name="Profile"
    component={Profile}
    options={{ drawerLabel: 'Profile' }}
  />
</Drawer.Navigator>);}

答案 4 :(得分:0)

我添加了导航设置代码,用于禁用特定屏幕的侧边菜单,这可能对某些人有帮助。

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { getFocusedRouteNameFromRoute } from '@react-navigation/native';

import LoginScreen from '../LoginScreen'
import PasswordScreen from '../PasswordScreen'
import HomeScreen from '../HomeScreen'


const Stack = createStackNavigator()
const Drawer = createDrawerNavigator()
const sideMenuDisabledScreens = ['Login', 'Password']

const DrawerNavigator = () => {
  return (
    <Drawer.Navigator 
      screenOptions={{headerShown: false}} 
      drawerPosition="right"
      drawerContent={(props) => <SideMenu {...props} />}
    >
    <Drawer.Screen 
      name="Initial"
      component={StackNavigator}
      options={({ route }) => {
        const routeName = getFocusedRouteNameFromRoute(route) ?? 'Login'
        if (sideMenuDisabledScreens.includes(routeName))
            return ({swipeEnabled: false})
      }} 
    />
    </Drawer.Navigator>
  );
}

const StackNavigator = () => {
  return (
    <Stack.Navigator screenOptions={{headerShown: false}}>
      <Stack.Screen name='Login' component={LoginScreen}/>
      <Stack.Screen name='Password' component={PasswordScreen} options={{gestureEnabled: false}}/>
      <Stack.Screen name='Home' component={HomeScreen} />
    </Stack.Navigator>
  );
}

function MainNavigator() {
  return (
    <NavigationContainer>
      <DrawerNavigator />
    </NavigationContainer>
  );
}

export default MainNavigator

App.js 有以下代码

import React, { Component } from 'react';
import MainStackNavigator from './src/navigation/MainStackNavigator';

export default class App extends Component {

  render() {
    return (
      <>
        <MainStackNavigator />
        </>
    );
  }
}