【问题标题】:React native navigation custom tabbar icon反应原生导航自定义标签栏图标
【发布时间】:2017-12-14 10:31:46
【问题描述】:

这可能有自定义组件(按钮或其他)而不是标签栏中的简单图标吗?

我需要像这样动态设置标签栏图标

this.props.navigator.setTabButton({
          tabIndex: 0,
          icon: <Icon name="heart" size={28} />  <--- not appear    
        });

或者我们只能使用图标?有什么解决办法吗?

【问题讨论】:

  • 嘿,你找到解决这个问题的方法了吗?
  • @Rawan,你不能这样做你能做的就是 - 在路由器安装上设置图标

标签: react-native react-native-android react-native-ios react-native-navigation


【解决方案1】:

是的,这是可能的。我将我的解决方案分享为以下代码:

const renderNav = (routeName, focused) => {
    return (
        <View style={{
            flex: 1,
            width: 90,
            justifyContent: 'center',
            alignItems: 'center',
            borderTopColor: StylesGeneric.COLORS.primary,
            borderTopWidth: focused ? 4 : 0,
        }}>
            <Text
                style={[StylesGeneric.STYLES.footerText]}>
                {routeName}
            </Text>
        </View>
    );
};

const customTabs = ({navigation}) => ({
    tabBarIcon: ({focused}) => {
        const {routeName} = navigation.state;
        if (routeName === 'Home') {
            return renderNav('Home', focused);
        } else if (routeName === 'Profile') {
            return renderNav('Profile', focused);
        } else if (routeName === 'ProfileOther') {
            return renderNav('ProfileOther', focused);
        }
    },
});

const nav = createBottomTabNavigator(
    {
        Home: {
            screen: Home,
        },
        Profile: {
            screen: Profile,
        },
        ProfileOther: {
            screen: ProfileOther,
        },
    },
    {
        defaultNavigationOptions: customTabs,
        animationEnabled: true,
        swipeEnabled: true,
        tabBarPosition: 'bottom',
        initialRouteName: 'Home',
        tabBarOptions: {
            showLabel: false,
            style: {
                borderTopColor: 'transparent',
                backgroundColor: StylesGeneric.COLORS.white,
                height: StylesGeneric.FOOTER_HEIGHT,
            },
        },
    },
);

const AppContainer = createAppContainer(nav);

【讨论】:

  • 感谢您的尝试!不幸的是,您的代码适用于 React Navigation,但我询问过 React Native Navigation
  • @АндрейГузюк 当我将这个库用于 React Native 项目时,我没有看到确切的库名称。很抱歉造成误解。
猜你喜欢
  • 2020-09-16
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
相关资源
最近更新 更多