【问题标题】:How to create gradient border color for React Navigation TabBar如何为 React Navigation TabBar 创建渐变边框颜色
【发布时间】:2021-09-08 11:48:16
【问题描述】:

我想在我的 TabBar 上放置一个细渐变边框。我尝试使用 BorderTopColor 样式简单地实现它,但 React Native 默认不支持渐变。所以我安装了www.npmjs.com/package/react-native-linear-gradient。但是,我不知道将渐变容器放置在我的 TabBar 和 Screens 之间。

这是我目前所拥有的:

const Tab = createBottomTabNavigator();

const App: () => Node = () => {
  const customTabBarStyle = {
    activeTintColor: '#ffffff',
    inactiveTintColor: '#b9b9b9',
    showLabel: false,
    style: {
      backgroundColor: '#1e1e1e',
      height: 200
    }
  };

  return (
    <NavigationContainer>
      <LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}} colors={['red', 'yellow']} style={{height: 2}} />
      <Tab.Navigator
        tabBarOptions = {customTabBarStyle}
        screenOptions={{ tabBarStyle: { backgroundColor: '#1e1e1e', borderTopWidth: 0, elevation: 0}, headerStyle: { backgroundColor: '#1e1e1e'}, headerTitleStyle: {color: 'white'}}}>
        <Tab.Screen
          name="PrimaryMarket"
          component={PrimaryMarketScreen}
          options={{
            tabBarIcon: ({color}) => (
              <Icon name="shopping-cart" size={40} color={color} />
            ),
          }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

这就是我想要的:

【问题讨论】:

    标签: react-native react-navigation gradient


    【解决方案1】:

    我有同样的问题,所以我查看了 react-navigation v6 的源代码来解决问题。在源代码中,我为tabBarBackground找到了这个:

    返回一个 React 元素用作背景的函数 标签栏。您可以渲染图像、渐变、模糊视图等。

    您可以在screenOptions 中使用tabBarBackground,如下所示:

    const Tab = createBottomTabNavigator();
    
    const App: () => Node = () => {
      const customTabBarStyle = {
        activeTintColor: "#ffffff",
        inactiveTintColor: "#b9b9b9",
        showLabel: false,
        style: {
          backgroundColor: "#1e1e1e",
          height: 200,
        },
      };
    
      return (
        <NavigationContainer>
          <Tab.Navigator
            tabBarOptions={customTabBarStyle}
            screenOptions={{
              tabBarStyle: {
                borderTopWidth: 0,
                elevation: 0,
              },
              headerStyle: { backgroundColor: "#1e1e1e" },
              headerTitleStyle: { color: "white" },
              tabBarBackground: () => (
                <View style={{ flex: 1 }}>
                  <LinearGradient
                    start={{ x: 0, y: 0 }}
                    end={{ x: 1, y: 0 }}
                    colors={["red", "yellow"]}
                    style={{ height: 2 }}
                  />
                </View>
              ),
            }}
          >
            <Tab.Screen
              name="PrimaryMarket"
              component={PrimaryMarketScreen}
              options={{
                tabBarIcon: ({ color }) => (
                  <Icon name="shopping-cart" size={40} color={color} />
                ),
              }}
            />
          </Tab.Navigator>
        </NavigationContainer>
      );
    };
    

    不同的代码,但我使用了相同的技术:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 2018-12-27
      • 2016-12-15
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多