如何使本机反应导航选项卡栏透明

Mon*_*key 7 react-native react-navigation

有没有办法使标签栏透明?我尝试了以下操作,但它只显示了白色背景。我需要实现自己的tabBarComponent吗?如果是这样,是否有关于该类的文档以及我需要实现的接口?

const MainTabNavigator = TabNavigator(
  {
    MessageCenter: { screen: MessageCenterStack },
    Camera: { screen: CameraStack },
  },
  {
    tabBarPosition: 'bottom',
    swipeEnabled: true,
    animationEnabled: true,
    tabBarOptions: {
      style: {
        backgroundColor:  'transparent',
      },
    }
  }
);
Run Code Online (Sandbox Code Playgroud)

Mon*_*key 9

我必须设置位置,absolute并为其设置一个左右和底部,以使backgroundColor透明生效。

tabBarOptions: {
  showIcon: true,
  showLabel: false,
  lazyLoad: true,
  style: {
    backgroundColor: 'transparent',
    borderTopWidth: 0,
    position: 'absolute',
    left: 50,
    right: 50,
    bottom: 20,
    height: 100
  }
}
Run Code Online (Sandbox Code Playgroud)


Moh*_*fik 7

我最终通过为自定义标签栏组件添加容器视图并使它在容器中绝对定位并使标签栏保持原样,使其在android和ios上正常工作

这是自定义标签栏组件

const TabBarComponent = (props) => (<BottomTabBar {...props} />)
Run Code Online (Sandbox Code Playgroud)

这是标签栏选项

{
    tabBarComponent: props => {
        return (
            <View style={{
                position: 'absolute',
                left: 0,
                right: 0,
                bottom: 0,
            }}>
                <TabBarComponent {...props} />
            </View>
        )
    },
    tabBarOptions: {
        style: {
            borderTopWidth: 0,
            backgroundColor: '#FFFFFF',
            borderTopRightRadius: 20,
            borderTopLeftRadius: 20,
            height: 55,
            paddingBottom: 5,
        }
    },
    initialRouteName: 'HomeScreen',
}
Run Code Online (Sandbox Code Playgroud)

最后的结果

具有边框半径的透明标签栏


kiw*_*des 7

对于这个问题,这里的许多答案似乎有点令人费解。因此,对于其他正在寻找如何操作的人,这里有一个简单的答案:

在标签栏选项中,将位置更改为绝对,将背景颜色更改为透明,如下所示:

tabBarOptions: {
  style: {
    backgroundColor: 'transparent',
    position: 'absolute',
    left: 0,
    bottom: 0,
    right: 0
  }
}
Run Code Online (Sandbox Code Playgroud)


Art*_* M. 7

这就是我为react-navigation v6解决这个问题的方法

import {
  createBottomTabNavigator,
  BottomTabBar,
} from '@react-navigation/bottom-tabs';
Run Code Online (Sandbox Code Playgroud)

我们需要使用BottomTabBar来自定义布局并使其透明

const Tab = createBottomTabNavigator();

<Tab.Navigator
      // Here under tabBar we customize the view and set the bg to transparent
      tabBar={props => (
        <View style={{ backgroundColor: 'transparent', position: 'absolute', left: 0, bottom: 0, right: 0 }}>
          <BottomTabBar {...props} />
        </View>
      )}>
...
Run Code Online (Sandbox Code Playgroud)

如果你在下面这样做

screenOptions={({ route }) => ({
   tabBarStyle:{
     position:absolute,
     ...
   }
 })
} 
Run Code Online (Sandbox Code Playgroud)

它没有按预期工作


Dha*_*ani 6

position: 'absolute' 是一个解决方案,但您可能会注意到它在 android 端看起来并不完美,但在 android 端完美运行。

终于,经过长时间的努力,我找到了解决方案。

海拔:0

在标签栏样式上设置此项将解决此问题。

例子 -

tabBarOptions={{
        showIcon: true,
        showLabel: true,
        activeTintColor: COLORS.tabSelected,
        inactiveTintColor: COLORS.tabNormal, 
        style: {
          backgroundColor:'transparent',
          borderTopWidth: 0,
          position: 'absolute',
          elevation: 0  // <-- this is the solution
        },
        labelStyle: {
          fontSize: 12,
        },
      }}>
Run Code Online (Sandbox Code Playgroud)

这是输出屏幕截图。 在设置“海拔:0”之前,它看起来像这样

设置“海拔:0”后,看起来很完美