如何仅在活动的Tab上显示TabBar标签?

Def*_*ian 2 reactjs react-native react-navigation

选项卡不活动时,我想隐藏TabBar标签。使用showLabelfrom tabBarOptions只能完全禁用它们。提前致谢。

材质设计标签栏

Seb*_*čić 7

Hey Drew thanks for the idea, couldn't figure it out on my own, but I think you have a lot of extra code unnecessary for the functionality asked in the question. Here is my take on this, and this works just as well.

export const BottomTabNavigator = createBottomTabNavigator(
  {
    News: {
      screen: NewsNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <NewsIconActive /> : <NewsIcon />
      }
    },
    Rewards: {
      screen: RewardsNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <RewardsIconActive /> : <RewardsIcon />
      }
    },
    Home: {
      screen: HomeNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <HomeIconActive /> : <HomeIcon />
      }
    },
    Leaderboard: {
      screen: LeaderboardNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <LeaderboardIconActive /> : <LeaderboardIcon />
      }
    },
    Profile: {
      screen: ProfileNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <ProfileIconActive /> : <ProfileIcon />
      }
    }
  },
  {
    initialRouteName: 'Profile'
    },
    navigationOptions: ({ navigation }) => ({
      tabBarLabel: ({ focused }) => {
        const { routeName } = navigation.state;
        switch (routeName) {
          case 'News':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          case 'Rewards':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          case 'Home':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          case 'Leaderboard':
            return focused ? (
              <Text >{routeName}</Text>
            ) : null;
            break;
          case 'Profile':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          default:
            return null;
            break;
        }
      }
    })
  }
);
Run Code Online (Sandbox Code Playgroud)


小智 6

这个相当简单的解决方案在 React Navigation 的第 5 版中对我有用:

<Tab.Navigator
    screenOptions={({ route, navigation }) => {
        return { tabBarLabel: navigation.isFocused() ? route.name : '' };
    }}
>
Run Code Online (Sandbox Code Playgroud)


Dre*_*dré 5

我也遇到了这个问题,但找到了一个可能对您有帮助的解决方案。看看我是如何配置的tabBarIcontabBarLabel如下:

const MainNavigator = createBottomTabNavigator({
    Home: HomeStack,
    MyProsthetic: MyProstheticStack,
    Appointments: AppointmentsStack,
    MyNotes: MyNotesStack,
    Contact: ContactStack,
 },
 {
 navigationOptions: ({ navigation }) => ({
  tabBarIcon: ({ focused, tintColor }) => {
    const { routeName } = navigation.state;
    let icon;
    switch(routeName) {
      case 'Home':
        tabBarLabel = true
        return icon = <Icon name='home' size={30} color={focused ? Colors.darkBlue : Colors.lightGrey} />
      case 'MyProsthetic':
        return icon = <Icon name='user' size={28} color={focused ? Colors.darkBlue : Colors.lightGrey} />
      case 'Appointments':
        return icon = <IonIcon name='md-calendar' size={32} color={focused ? Colors.darkBlue : Colors.lightGrey} style={{position: 'relative', top: 3}}/>
      case 'MyNotes':
        return icon = <Icon name='file' size={23} color={focused ? Colors.darkBlue : Colors.lightGrey} />
      case 'Contact':
        return icon = <Icon name='phone' size={30} color={focused ? Colors.darkBlue : Colors.lightGrey} />

    }
    return icon
  },
  tabBarLabel: ({ focused, tintColor }) => {
    const { routeName } = navigation.state;
    let label;
    switch(routeName) {
      case 'Home':
        return label = focused ? <Text style={styles.activeTabText}>Home</Text> : null
      case 'MyProsthetic':
        return label = focused ? <Text style={styles.activeTabText}>My Prosthetic</Text> : null
      case 'Appointments':
        return label = focused ? <Text style={styles.activeTabText}>Appointments</Text> : null
      case 'MyNotes':
        return label = focused ? <Text style={styles.activeTabText}>Notes</Text> : null
      case 'Contact':
        return label = focused ? <Text style={styles.activeTabText}>Contact</Text> : null
    }
    return label
  },
}),
lazy: false,
tabBarOptions: {
  style: {
    paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight : 0,
    borderTopWidth: 1,
    borderTopColor: Colors.lightGrey
  },
}
}
)
Run Code Online (Sandbox Code Playgroud)