自定义标签栏React Navigation 5

时间:2020-05-28 18:19:58

标签: react-native react-navigation react-navigation-v5

我正在尝试使用React Navigation制作一个如图所示的标签栏。

我尝试了一些代码,但是没有任何效果。一些代码来自以前的React Navigation版本。真正的问题是,使选项卡栏的底部和两侧都有边距,并且边框是圆形的。

有人可以帮助我吗?!

enter image description here

2 个答案:

答案 0 :(得分:14)

这是演示:https://snack.expo.io/@nomi9995/createbottomtabnavigator-%7C-react-navigation

您可以使用tabBar道具来制作自定义TABBAR

<NavigationContainer>
      <Tab.Navigator tabBar={props => <MyTabBar {...props} />}>
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>

MyTabBar组件

function MyTabBar({ state, descriptors, navigation }) {
  return (
    <View style={{ flexDirection: 'row',backgroundColor:"#F4AF5F",height:50,borderRadius:50,justifyContent:"center",alignItems:"center" }}>
      {state.routes.map((route, index) => {
        const { options } = descriptors[route.key];
        const label =
          options.tabBarLabel !== undefined
            ? options.tabBarLabel
            : options.title !== undefined
            ? options.title
            : route.name;

        const isFocused = state.index === index;

        const onPress = () => {
          const event = navigation.emit({
            type: 'tabPress',
            target: route.key,
          });

          if (!isFocused && !event.defaultPrevented) {
            navigation.navigate(route.name);
          }
        };

        const onLongPress = () => {
          navigation.emit({
            type: 'tabLongPress',
            target: route.key,
          });
        };

        return (
          <TouchableOpacity
            accessibilityRole="button"
            accessibilityStates={isFocused ? ['selected'] : []}
            accessibilityLabel={options.tabBarAccessibilityLabel}
            testID={options.tabBarTestID}
            onPress={onPress}
            onLongPress={onLongPress}
            style={{ flex: 1 }}
          >
            <Text style={{ color: isFocused ? '#673ab7' : '#222' }}>
              {label}
            </Text>
          </TouchableOpacity>
        );
      })}
    </View>
  );
}

答案 1 :(得分:4)

此操作的关键是将样式position: 'absolute'添加到自定义TabBar的外部<View>容器中。这将消除白色背景问题。

相关问题