标签导航器

https://reactnavigation.org/docs/tab-based-navigation

  1. npm install @react-navigation/bottom-tabs

基于选项卡的导航的最小示例

  1. import * as React from 'react';
  2. import { Text, View } from 'react-native';
  3. import { NavigationContainer } from '@react-navigation/native';
  4. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
  5. function HomeScreen() {
  6. return (
  7. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  8. <Text>Home!</Text>
  9. </View>
  10. );
  11. }
  12. function SettingsScreen() {
  13. return (
  14. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  15. <Text>Settings!</Text>
  16. </View>
  17. );
  18. }
  19. const Tab = createBottomTabNavigator();
  20. const App = () => {
  21. return (
  22. <NavigationContainer>
  23. <Tab.Navigator>
  24. <Tab.Screen name="Home" component={HomeScreen} />
  25. <Tab.Screen name="Settings" component={SettingsScreen} />
  26. </Tab.Navigator>
  27. </NavigationContainer>
  28. );
  29. }
  30. export default App;

自定义外观及徽章

https://github.com/oblador/react-native-vector-icons
https://ionic.io/ionicons

  1. npm install --save react-native-vector-icons
  2. npm install @types/react-native-vector-icons
  1. npx react-native link react-native-vector-icons
  2. npx react-native start –reset-cache
  3. npx react-native run-ios
  1. import * as React from 'react';
  2. import { Text, View, Platform } from 'react-native';
  3. import { NavigationContainer } from '@react-navigation/native';
  4. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
  5. import Ionicons from 'react-native-vector-icons/Ionicons';
  6. function HomeScreen() {
  7. return (
  8. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  9. <Text>Home!</Text>
  10. </View>
  11. );
  12. }
  13. function PersonScreen() {
  14. return (
  15. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  16. <Text>Person!</Text>
  17. </View>
  18. );
  19. }
  20. function SettingsScreen() {
  21. return (
  22. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  23. <Text>Settings!</Text>
  24. </View>
  25. );
  26. }
  27. const Tab = createBottomTabNavigator();
  28. const App = () => {
  29. return (
  30. <NavigationContainer>
  31. <Tab.Navigator
  32. screenOptions={({ route }) => ({
  33. tabBarActiveTintColor: 'green',
  34. tabBarInactiveTintColor: 'gray',
  35. tabBarIcon: ({ focused, color, size }) => {
  36. let iconName: string;
  37. if (route.name === 'Home') {
  38. iconName = focused? 'chatbubbles' : 'chatbubbles-outline';
  39. } else if (route.name === 'Settings') {
  40. iconName = focused ? 'settings' : 'settings-outline';
  41. } else if (route.name === 'Person') {
  42. iconName = focused ? 'person' : 'person-outline';
  43. } else {
  44. iconName = ""
  45. }
  46. // You can return any component that you like here!
  47. return <Ionicons name={iconName} size={size} color={color} />;
  48. },
  49. })}
  50. >
  51. <Tab.Screen name="Home" component={HomeScreen} options={{ tabBarBadge: 3 }}/>
  52. <Tab.Screen name="Person" component={PersonScreen} />
  53. <Tab.Screen name="Settings" component={SettingsScreen} options={{
  54. tabBarBadgeStyle: {
  55. top: Platform.OS === "ios" ? 4 : 9,
  56. left: 4,
  57. minWidth: 10,
  58. maxHeight: 10,
  59. borderRadius: 5,
  60. fontSize: 6,
  61. lineHeight: 9,
  62. alignSelf: undefined,
  63. },
  64. tabBarBadge: ""
  65. }}/>
  66. </Tab.Navigator>
  67. </NavigationContainer>
  68. );
  69. }
  70. export default App;