【问题标题】:React-navigation: how to style a big middle button in tab navigation?React-navigation:如何在标签导航中设置大中间按钮的样式?
【发布时间】:2019-04-19 18:52:33
【问题描述】:

所以我有这个要求。以这种精确的外观制作标签导航:

使用渐变和按钮对标签栏进行样式设置没有问题。我用这段代码创建了自己的自定义代码:

export default createBottomTabNavigator({
  (... routes here)
}, {
  initialRouteName: "Inspiration",
  tabBarComponent: props => <BottomTabBar {...props} />
})

但现在我在使用中间按钮时遇到了问题。我的酒吧是这样的:

如果我删除了删除此行的自定义标签栏:

tabBarComponent: props => <BottomTabBar {...props} />

那么现在我的中间按钮看起来应该是这样的,但是当然,所有其他样式都丢失了:

这就是我的 BottomTabBar 组件现在的样子:

import React from "react";
import { Image, StyleSheet, Text, TouchableOpacity } from "react-native";
import { TabBarBottomProps, NavigationRoute } from "react-navigation";
import LinearGradient from "react-native-linear-gradient";

const iconBag = require("./images/bag.png");

export default function BottomTabBar(props: TabBarBottomProps) {
  const {
    renderIcon,
    getLabelText,
    activeTintColor,
    inactiveTintColor,
    onTabPress,
    onTabLongPress,
    getAccessibilityLabel,
    navigation
  } = props;

  const { routes, index: activeRouteIndex } = navigation.state;

  function renderTabBarButton(routeIndex, route) {
    const isRouteActive = routeIndex === activeRouteIndex;
    const tintColor = isRouteActive ? activeTintColor : inactiveTintColor;

    if (route.key == "Bag")
      return <Image style={{ width: 100, height: 100 }} source={iconBag} />;
    return (
      <TouchableOpacity
        key={routeIndex}
        style={styles.tabButton}
        onPress={() => onTabPress({ route })}
        onLongPress={() => onTabLongPress({ route })}
        accessibilityLabel={getAccessibilityLabel({ route })}
      >
        {renderIcon({ route, focused: isRouteActive, tintColor })}
        <Text style={styles.tabText}>{getLabelText({ route })}</Text>
      </TouchableOpacity>
    );
  }

  return (
    <LinearGradient colors={["#FFFFFF", "#DEDEDE"]} style={styles.container}>
      {routes.map((route, routeIndex) => renderTabBarButton(routeIndex, route))}
    </LinearGradient>
  );
}

const styles = StyleSheet.create({
  container: {
    height: 60,
    flexDirection: "row",
    alignItems: "center",
    borderWidth: 1,
    borderColor: "#C4C4C4"
  },
  tabButton: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  },
  tabText: {
    fontFamily: "Source Sans Pro",
    fontSize: 11,
    color: "#454545",
    marginTop: 1
  }
});

我能做什么?非常感谢任何帮助!

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:


    你可以试试我的解决方案

    const TabNavigator = createBottomTabNavigator(
    {
    Top: {
      screen: HomeScreen,
      navigationOptions: {
        tabBarIcon: ({tintColor}) => (
          <Image
            source={require('./src/assets/ic_list.png')}
            style={{width: 24, height: 24, tintColor: tintColor}}
          />
        ),
      },
    },
    
    New: {
      screen: HomeScreen,
      navigationOptions: {
        tabBarIcon: ({tintColor}) => (
          <Image
            source={require('./src/assets/ic_clock.png')}
            style={{width: 24, height: 24, tintColor: tintColor}}
          />
        ),
      },
    },
    Ask: { // big plus icon in the middle
      screen: HomeScreen,
      navigationOptions: {
        tabBarIcon: ({tintColor}) => (
          <View
            style={{
              position: 'absolute',
              bottom: 20, // space from bottombar
              height: 58,
              width: 58,
              borderRadius: 58,
              backgroundColor: '#5a95ff',
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Image
              source={require('./src/assets/ic_add_24.png')}
              style={{
                width: 40,
                height: 40,
                tintColor: '#f1f6f9',
                alignContent: 'center',
              }}
            />
          </View>
        ),
      },
    },
    Show: {
      screen: HomeScreen,
      navigationOptions: {
        tabBarIcon: ({tintColor}) => (
          <Image
            source={require('./src/assets/ic_notification.png')}
            style={{width: 24, height: 24, tintColor: tintColor}}
          />
        ),
      },
    },
    Jobs: {
      screen: HomeScreen,
      navigationOptions: {
        tabBarIcon: ({tintColor}) => (
          <Image
            source={require('./src/assets/ic_person.png')}
            style={{width: 24, height: 24, tintColor: tintColor}}
          />
        ),
      },
    },
    },
    {
    tabBarOptions: {
      activeTintColor: '#FF6F00',
      inactiveTintColor: '#8997B0',
      showIcon: true,
      showLabel: false,
      style: {
        backgroundColor: '#f1f6f9',
      },
    },
    },
    );
    

    【讨论】:

    • 这太棒了,它拯救了我的一天!感谢您的出色解决方案!
    • 谢谢,但是当我单击此图标“中间大加号图标”时,它不会将我导航到他的屏幕!特别是在 android 中!
    • @OliverD 这只是 UI 展示。为了导航到屏幕,您必须自己实现
    • 这里的问题不是导航。栏上方的按钮部分不可点击。即使我用 Touchable 包围整个 View。有什么想法吗?
    【解决方案2】:

    我用 react native 制作了这个底部标签。我觉得你的设计很简单。我认为我的代码示例会对您有所帮助。

    import React from 'react';
    import { createBottomTabNavigator, createStackNavigator } from 'react-navigation';
    import { View, Image } from 'react-native'
    import { Text } from 'native-base';
    
    import Featured from './featured';
    import AboutUs from './about_us';
    import Shop from './shop';
    import Booking from './booking';
    import Settings from './settings';
    import styles from './styles';
    
    import star from './../../assets/images/icons/star.png';
    import star_check from './../../assets/images/icons/star_check.png';
    import about from './../../assets/images/icons/about.png';
    import about_check from './../../assets/images/icons/about_check.png';
    import book from './../../assets/images/icons/book.png';
    import check from './../../assets/images/icons/check.png';
    import shop from './../../assets/images/icons/shop.png';
    import shop_check from './../../assets/images/icons/shop_check.png';
    import more from './../../assets/images/icons/more.png';
    import more_check from './../../assets/images/icons/more_check.png';
    
    const Tabs = createBottomTabNavigator(
        {
            Featured: {
                screen: Featured,
                navigationOptions: {
                    title: 'Featured',
                    tabBarIcon: ({ tintColor, focused }) => (
                            <View style={styles.tab}>
                                <Image source={focused? star_check : star} style={styles.icon} />
                                <Text style={[styles.name, {color: tintColor}]}>Kampanjer</Text>
                            </View>
                        )
                }
            },
            AboutUs: {
                screen: AboutUs,
                navigationOptions: {
                    title: 'About Us',
                    tabBarIcon: ({ tintColor, focused }) => (
                                <View style={styles.tab}>
                                    <Image source={focused? about_check : about} style={styles.icon} />
                                    <Text style={[styles.name, {color: tintColor}]}>Om oss</Text>
                                </View>
                            )
                }
            },
            Booking: {
                screen: Booking,
                navigationOptions: {
                    title: 'MIN SALONG',
                    tabBarIcon: ({ tintColor, focused }) => (
                                <View style={styles.book}>
                                    <Image source={focused? check : book} style={styles.book_icon} />
                                </View>
                            )
                }
            },
            Shop: {
                screen: Shop,
                navigationOptions: {
                    title: 'Shop',
                    tabBarIcon: ({ tintColor, focused }) => (
                                <View style={styles.tab}>
                                    <Image source={focused? shop_check : shop} style={styles.icon} />
                                    <Text style={[styles.name, {color: tintColor}]}>Shop</Text>
                                </View>
                            )
                }
            },
            Settings: {
                screen: Settings,
                navigationOptions: {
                    title: 'More',
                    tabBarIcon: ({ tintColor, focused }) => (
                                <View style={styles.tab}>
                                    <Image source={focused? more_check : more} style={styles.icon} />
                                    <Text style={[styles.name, {color: tintColor}]}>Inställningar</Text>
                                </View>
                            )
                }
            },
        },
        {
            initialRouteName: 'Featured',
            tabBarOptions: {
                activeTintColor: '#80A0AB',
                inactiveTintColor: '#fff',
                showLabel: false,
                style: {
                    height: 60,
                    backgroundColor: '#485155'
                },
                labelStyle: {
                    fontSize: 12,
                    fontFamily: 'Abel-Regular'
                }
            }  
        }
    );
    
    export default createStackNavigator({Tabs}, {headerMode: "none"});
    

    【讨论】:

    • 谢谢先生!问题是:我需要将渐变添加到栏,只有在我创建自己的标签栏时才有可能。然后,当我创建标签栏时,中间按钮无法正确显示
    • 我认为在 tabBarOptions 中可以设置渐变背景。然后你必须使用 react 组件自定义 tabBarIcon。
    • 您也可以分享一下样式吗?谢谢
    • @ProDev 能否分享一下中间按钮的样式,谢谢
    • 如果没有书本图标的样式,这个回复似乎没有太大帮助...有人弄清楚了吗?
    【解决方案3】:

    试试这个

    <Tab.Navigator
                tabBarOptions={{
                    activeTintColor: 'red',
                   // activeBackgroundColor:'#000',
                    //inactiveBackgroundColor:'red',
                    labelStyle: {
                         position: 'absolute',
                         top: constants.vh(35),
                        fontSize:constants.vh(18),
                    }
                }}
            >
                <Tab.Screen name='Home' //bottom tab for Home
                    options={{
                        tabBarIcon: ({ focused }) => {
                            let iconName;
                            iconName = focused ? constants.images.bottomHome : constants.images.bottomHome //for icon or image
                            return (
                                <View>
                                    <Image source={iconName} style={{ width: constants.vw(40), height: constants.vh(25) ,position:'absolute',right:constants.vw(-20),bottom:constants.vh(-5)}} resizeMode="contain" />
                                </View>
                            )
                        }
                    }}
                    component={HomeScreen} />
    </Tab.Navigator>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多