DrawerNavigator始终显示选项卡栏,同时点按每个选项

时间:2020-10-02 07:49:16

标签: react-native react-navigation navigation-drawer react-navigation-bottom-tab

我试图有一个标签栏,每个标签栏都有自己的堆栈导航器。我拥有的三个标签是 家 个人资料 设置

我也想在抽屉中显示这三个选项。我已经创建了一个抽屉,但是只有在家里点击才能显示选项卡栏。我想显示标签栏,就像您按个人资料标签一样,而标签栏仍在那里。

Tabs with their stack navigators

Side drawer with same options

Tapping profile from drawer hides tab bar

这是我的代码:

const HomeStackNavigator = createStackNavigator();
export const HomeNavigator = () => {
  return (
    <HomeStackNavigator.Navigator screenOptions={defaultNavOptions}>
      <HomeStackNavigator.Screen
        name="Home"
        component={HomeScreen}
        options={homeScreenOptions}
      />
       <HomeStackNavigator.Screen
        name="Details"
        component={DetailsScreen}
        options={detailsScreenOptions}
      />
    </HomeStackNavigator.Navigator>
  );
};

const ProfileStackNavigator = createStackNavigator();
export const ProfileNavigator = () => {
  return (
    <ProfileStackNavigator.Navigator screenOptions={defaultNavOptions}>
      <ProfileStackNavigator.Screen
        name="Profile"
        component={ProfileScreen}
        options={profileScreenOptions}
      />
       <ProfileStackNavigator.Screen
        name="EditProfile"
        component={EditProfileScreen}
        options={editProfileScreenOptions}
      />
    </ProfileStackNavigator.Navigator>
  );
};

const SettingsStackNavigator = createStackNavigator();
export const SettingsNavigator = () => {
  return (
    <SettingsStackNavigator.Navigator screenOptions={defaultNavOptions}>
      <SettingsStackNavigator.Screen
        name="Settings"
        component={SettingsScreen}
        options={settingsScreenOptions}
      />
       <SettingsStackNavigator.Screen
        name="AccountDetail"
        component={AccountDetailsScreen}
        options={accountDetailsScreenOptions}
      />
    </SettingsStackNavigator.Navigator>
  );
};




const HomeTabNavigator = createBottomTabNavigator();
export const TabNavigator = () => {
  return (
    <HomeTabNavigator.Navigator screenOptions={defaultNavOptions}>
      <HomeTabNavigator.Screen
        name="Home"
        component={HomeNavigator}
        options={{
          tabBarLabel: 'Home',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="home" color={color} size={size} />
          ),
        }}
      />
       <HomeTabNavigator.Screen
        name="Profile"
        component={ProfileNavigator}
        options={{
          tabBarLabel: 'Profile',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="face-profile" color={color} size={size} />
          ),
        }}
      />
      <HomeTabNavigator.Screen
        name="Settings"
        component={SettingsNavigator}
        options={{
          tabBarLabel: 'Settings',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="account-settings" color={color} size={size} />
          ),
        }}
      />
    </HomeTabNavigator.Navigator>
  );
};



const AppDrawer = createDrawerNavigator();
export const Drawer = () => {
  return(
      <AppDrawer.Navigator initialRouteName="Home">
        <AppDrawer.Screen name="Home" component={TabNavigator} />
        <AppDrawer.Screen name="Profile" component={ProfileNavigator} />
        <AppDrawer.Screen name="Settings" component={SettingsNavigator} />
      </AppDrawer.Navigator>
  )
};

我的目标是一直使用标签。仅当我们进入任何选项卡的详细信息页面时,选项卡才应隐藏。

1 个答案:

答案 0 :(得分:1)

您可以将TabNavigator设置为堆栈导航器的屏幕(即抽屉式导航器的屏幕),然后将自定义抽屉组件传递到抽屉式导航器:

const AppDrawer = createDrawerNavigator();
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
const HomeTabNavigator = createBottomTabNavigator();

export const TabNavigator = () => {
  return (
    <HomeTabNavigator.Navigator>
      <HomeTabNavigator.Screen
        name="Home"
        component={HomeScreen}
        options={{
          tabBarLabel: 'Home',
        }}
      />
      <HomeTabNavigator.Screen
        name="Profile"
        component={ProfileScreen}
        options={{
          tabBarLabel: 'Profile',
        }}
      />
      <HomeTabNavigator.Screen
        name="Settings"
        component={SettingsScreen}
        options={{
          tabBarLabel: 'Settings',
        }}
      />
    </HomeTabNavigator.Navigator>
  );
};

function CustomDrawerContent(props) {
  return (
    <DrawerContentScrollView {...props}>
      <DrawerItem
        label="Home"
        onPress={() => props.navigation.navigate('Home')}
      />
      <DrawerItem
        label="Profile"
        onPress={() => props.navigation.navigate('Profile')}
      />
      <DrawerItem
        label="Settings"
        onPress={() => props.navigation.navigate('Settings')}
      />
    </DrawerContentScrollView>
  );
}

function getHeaderTitle(route) {
  return getFocusedRouteNameFromRoute(route) ?? 'Home';
}

const StackNavigator = () => {
  return (
    <Stack.Navigator>
      <Stack.Screen
        name="Tabs"
        component={TabNavigator}
        options={({route}) => ({
          headerTitle: getHeaderTitle(route),
        })}
      />
      <Stack.Screen name="EditProfile" component={EditProfileScreen} />
      <Stack.Screen name="Details" component={DetailsScreen} />
      <Stack.Screen name="AccountDetail" component={AccountDetailsScreen} />
    </Stack.Navigator>
  );
};

export const Drawer = () => {
  return (
    <AppDrawer.Navigator
      initialRouteName="Home"
      drawerContent={(props) => <CustomDrawerContent {...props} />}>
      <AppDrawer.Screen name="Stack" component={StackNavigator} />
    </AppDrawer.Navigator>
  );
};

function App() {
  return (
    <NavigationContainer>
      <Drawer />
    </NavigationContainer>
  );
}

您不想在其中显示选项卡的屏幕可以放在选项卡导航器之外的堆栈导航器中。


来源:

请确保从DrawerItem导入DrawerContentScrollView@react-navigation/drawer,并从getFocusedRouteNameFromRoute导入@react-navigation/native.