[英]Using FontAwesome in a tabbar with React Native
I am new to react native and am trying to create a tabbar using createBottomTabNavigator.我是本机反应的新手,正在尝试使用 createBottomTabNavigator 创建一个标签栏。 I would like each tab to have its own icon.
我希望每个标签都有自己的图标。
I have followed the following tutorial which uses FontAwesome to display the tab icon.我遵循了以下使用 FontAwesome 显示选项卡图标的教程。
When I run my app in the iso simulator the tabs display but the icons don't.当我在 iso 模拟器中运行我的应用程序时,选项卡显示但图标不显示。
Here is my code这是我的代码
import { createStackNavigator } from 'react-navigation-stack'; import { createAppContainer } from 'react-navigation'; import { createBottomTabNavigator } from 'react-navigation-tabs'; import Icon from "react-native-vector-icons/FontAwesome5"; import HomeScreen from './HomeScreen'; import SecondActivity from './second'; const TabNavigator = createBottomTabNavigator({ Home: { screen: HomeScreen, defaultNavigationOptions: { tabBarIcon: ({tintColor}) => <Icon name="home" size={25} color={tintColor} /> } }, Events: { screen: SecondActivity, defaultNavigationOptions: { tabBarIcon: ({tintColor}) => <Icon name="chart-bar" size={25} color={tintColor} /> } } } ); const MyStack = createStackNavigator({ Tabs: { screen: TabNavigator }, Home: { screen: HomeScreen }, Events: { screen: SecondActivity } }, { initialRouteName: 'Tabs', } ); export default createAppContainer(MyStack);
How do I get the icons to display?如何让图标显示出来?
I have a solution for you that is a bit different that what you did in there .. so我为您提供了一个与您在那里所做的有点不同的解决方案..所以
const config = Platform.select({
web: { headerMode: "screen" },
default: {}
})
const HomeStack = createStackNavigator(
{
Home: HomeScreen
},
config
)
HomeStack.navigationOptions = {
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === "ios" ? "ios-calendar" : "md-calendar"}
/>
)
}
HomeStack.path = ""
and them you can do like this in createBottomTabNavigator你可以在 createBottomTabNavigator 中这样做
const tabNavigator = createBottomTabNavigator(
{
HomeStack,
},
{
activeColor: '#000'
}
// You can import Ionicons from @expo/vector-icons if you use Expo or // react-native-vector-icons/Ionicons otherwise. import Ionicons from 'react-native-vector-icons/Ionicons'; import { createAppContainer } from 'react-navigation'; import { createBottomTabNavigator } from 'react-navigation-tabs'; export default createBottomTabNavigator( { Home: HomeScreen, Settings: SettingsScreen, }, { defaultNavigationOptions: ({ navigation }) => ({ tabBarIcon: ({ focused, horizontal, tintColor }) => { const { routeName } = navigation.state; let IconComponent = Ionicons; let iconName; if (routeName === 'Home') { iconName = focused ? 'ios-information-circle' : 'ios-information-circle-outline'; // Sometimes we want to add badges to some icons. // You can check the implementation below. IconComponent = HomeIconWithBadge; } else if (routeName === 'Settings') { iconName = focused ? 'ios-list-box' : 'ios-list'; } // You can return any component that you like here! return <IconComponent name={iconName} size={25} color={tintColor} />; }, }), tabBarOptions: { activeTintColor: 'tomato', inactiveTintColor: 'gray', }, } );
Link : https://snack.expo.io/@react-navigation/basic-tabs-v3链接: https : //snack.expo.io/@react-navigation/basic-tabs-v3
Link : https://reactnavigation.org/docs/en/tab-based-navigation.html链接: https : //reactnavigation.org/docs/en/tab-based-navigation.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.