反应原生导航自定义标签栏图标

时间:2017-12-14 10:31:47

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

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

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

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

或者我们只能使用图标?任何解决方案?

1 个答案:

答案 0 :(得分: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);