如何在底部标签导航器中显示徽章?

时间:2020-07-30 18:22:54

标签: reactjs react-native native

我的React Native应用程序使用底部标签导航器。我想尽可能显示带有数字的徽章作为通知。我使用mobx商店进行状态管理。如何在标签导航器中显示徽章?

这是我的底部标签导航器的代码。

const Tab = createBottomTabNavigator();

export default TAB1 = () => {
  return (
    
      <Tab.Navigator style={{ height:50 }}
screenOptions={({ route }) => ({
  headerShown: true  ,     
  tabBarIcon: ({ focused, color, size }) => {
            let iconName;

            if (route.name === 'Browse')
            {
              iconName = focused ? 'checkbox-blank-circle' : 'checkbox-blank-circle-outline';
            }
            else if (route.name === 'Find deals')
            {
              iconName = focused ? 'clipboard-alert' : 'clipboard-alert-outline';
            }
            else if (route.name === 'Favorites')
            {
              iconName = focused ?  'checkbox-multiple-blank-circle' : 'checkbox-multiple-blank-circle-outline';
            }
            else if (route.name === 'More')
            {
              iconName = focused ? 'checkbox-multiple-marked-circle' : 'checkbox-multiple-marked-circle-outline' ;
            }

            // You can return any component that you like here!
            return <Icon name={iconName} size={25} color='rgb(68,68,68)' style={{ marginTop:5 }}/>;
          },
        })}
        tabBarOptions={{
          keyboardHidesTabBar: true,
          activeTintColor: 'rgb(30,30,30)',
          inactiveTintColor: 'rgb(68,68,68)',
          //activeBackgroundColor:'',
          //inactiveBackgroundColor:'',
          style : { height:52 },
          tabStyle: { paddingVertical: 2, },
          labelStyle: {
            fontSize: 12,
            margin: 0,
            padding: 0,
          },
        }}

      >
        <Tab.Screen name="Browse" component={PLACEcard1} />
        <Tab.Screen name="Find deals" component={PLACEcard2} />
        <Tab.Screen name="Favorites" options={{headerShown: false}} component={SCREEN1} />
        <Tab.Screen name="More" component={MODAL3} />
      </Tab.Navigator>
    
  );
}

1 个答案:

答案 0 :(得分:0)

Ciao,在您的tabBarIcon上,如果您不想自己制作徽章图标,则可以返回Icon,例如,返回react-native-icon-badge。然后,使用mobx,您可以隐藏/显示徽章,设置/重置数字或文本。我的意思是:

tabBarIcon: ({ focused, color, size }) => {
        ...
        return (
           <View>
              <Icon name={iconName} size={25} color='rgb(68,68,68)' style={{ marginTop:5 }}/>
              <IconBadge
                MainElement={
                   <View>
                      // customize your badge icon
                   </View>
                }
                BadgeElement={
                   // here your text in badge icon
                   <Text style={{color:'#FFFFFF'}}>{/*here mobx state to notify for example a number*/}</Text>
                }
                IconBadgeStyle={
                  // here the style 
                  }
                Hidden={/*here mobx state show/hide*/}
               />
           </View>
        );
      },