5

我更改了背景颜色以使其更明显。我希望红色容器变得透明。在此处输入图像描述

有什么办法可以做到这一点?我正在使用 react-navigation 5,并根据 Bottom-tab-navigator创建了一个自定义底部标签栏

我用于底栏的代码如下

import React from 'react';
import { View, StyleSheet } from 'react-native';
import colors from 'styles/colors';
import TabBarItem from './TabBarItem/TabBarItem';
const homeIcon = require('assets/maps.png');

export enum Item {
  Home,
  ProfileView,
}

const DashboardTabBar: React.FC<{}> = ({ state, descriptors, navigation }) => {
  return (
    <View style={styles.container}>
      <View style={styles.innerContainer}>
        {state.routes.map((route, index) => {
          const { options } = descriptors[route.key];

          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 (
            <TabBarItem
              icon={homeIcon}
              isFocused={isFocused}
              onPress={onPress}
              onLongPress={onLongPress}
              options={options}
              key={route.key}
            />
          );
        })}
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    padding: 10,
    paddingBottom: 18,
    backgroundColor: 'red', // I tried with 'transparent' here, but i see a white background instead of transparent
  },
  innerContainer: {
    height: 60,
    backgroundColor: colors.GREY_L_10,
    flexDirection: 'row',
    justifyContent: 'space-evenly',
    alignItems: 'center',
    borderRadius: 50,
    borderWidth: 1,
    borderColor: colors.GREY,
  },
});

export default DashboardTabBar;

就我查看代码而言,我找不到任何使其透明的方法。

4

4 回答 4

6

在您的代码的其他地方,您有一个使用 DashboardTabBar 组件的组件。您应该将带有样式对象的tabBarOptions道具添加到Tab.Navigator组件,如下所示:

    <Tab.Navigator
      tabBar={...}
      tabBarOptions={{
        style: {
          backgroundColor: 'transparent',
          position: 'absolute',
          left: 0,
          right: 0,
          bottom: 0,
          elevation: 0, <----to get rid of a shadow problem on Android
        },
      }}>
    { /* ...your screens go here */ }
</Tab.Navigator>

我在 iOS 和 Android 上都成功地做到了这一点。就个人而言,它不适合我的应用程序。 在此处输入图像描述 在此处输入图像描述

于 2020-04-05T08:20:04.003 回答
3

默认情况下,返回的 NavigatorcreateBottomTabNavigator不会与 TabBar 重叠屏幕。话虽如此,即使您的 TabBar 是透明的,您的 Screen 也会在 TabBar 开始的地方结束。屏幕不在 TabBar 后面

幸运的是,您需要做的就是添加position: 'absolute', bottom: 0, left: 0, right: 0到您的 TabBarcontainer样式(您申请的样式backgroundColor: 'transparent'

于 2020-04-05T00:49:20.187 回答
0
const styles = StyleSheet.create({
  container: {
    padding: 10,
    paddingBottom: 18,
    backgroundColor: 'transparent',
  },
  innerContainer: {
    height: 60,
    backgroundColor: colors.GREY_L_10,
    flexDirection: 'row',
    justifyContent: 'space-evenly',
    alignItems: 'center',
    borderRadius: 50,
    borderWidth: 1,
    borderColor: colors.GREY,
  },
});
于 2020-04-02T06:10:15.020 回答
0

对于 React Navigation v6,您可能希望在 TabNavigator 上设置screenOptions(我将它与自定义/透明底部标签栏结合使用)。

import {
  BottomTabBar,
  createBottomTabNavigator,
} from '@react-navigation/bottom-tabs';

const Tab = createBottomTabNavigator();

<Tab.Navigator
      screenOptions={{
        tabBarStyle: {backgroundColor: 'blue'},
      }}
      tabBar={props => {
        return (
          <View>
            <BottomTabBar {...props} />
          </View>
        );
      }}
      initialRouteName={SCREEN_NAMES.APP.HOME_TAB}
      ...
  </Tab.Navigator>

在此处输入图像描述

于 2021-09-30T13:58:05.703 回答