• react-navigation 6.x 学习(3)


    Tab navigation

    移动应用中最常见的导航是基于Tab的导航。这可以是屏幕底部的标签,也可以标题下面的标签(甚至可以代替标题)。

    本指南涵盖了createBottomTabNavigator。您也可以使用createMaterialBottomTabNavigator和createMaterialTopTabNavigator来为您的应用程序添加标签。

    在继续之前,首先安装@react-navigation/bottom-tabs:

    npm install @react-navigation/bottom-tabs
    yarn add @react-navigation/bottom-tabs
    
    • 1
    • 2

    基于tab导航的实例

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

    自定义外观

    这类似于您定制堆栈导航器的方式——有些属性是在初始化选项卡导航器时设置的,而其他属性则可以在每个屏幕的选项(options)中定制。

    // You can import Ionicons from @expo/vector-icons/Ionicons if you use Expo or
    // react-native-vector-icons/Ionicons otherwise.
    import Ionicons from 'react-native-vector-icons/Ionicons';
    
    // (...)
    
    export default function App() {
      return (
        <NavigationContainer>
          <Tab.Navigator
            screenOptions={({ route }) => ({
              tabBarIcon: ({ focused, color, size }) => {
                let iconName;
    
                if (route.name === 'Home') {
                  iconName = focused
                    ? 'ios-information-circle'
                    : 'ios-information-circle-outline';
                } else if (route.name === 'Settings') {
                  iconName = focused ? 'ios-list-box' : 'ios-list';
                }
    
                // You can return any component that you like here!
                return <Ionicons name={iconName} size={size} color={color} />;
              },
              tabBarActiveTintColor: 'tomato',
              tabBarInactiveTintColor: 'gray',
            })}
          >
            <Tab.Screen name="Home" component={HomeScreen} />
            <Tab.Screen name="Settings" component={SettingsScreen} />
          </Tab.Navigator>
        </NavigationContainer>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    1.tabBarIcon是底部标签导航器中支持的选项。所以我们可以在屏幕(Screen)组件的options属性中使用它,但在这里我们选择把它放在Tab的screenOptions属性中。导航器以便集中配置图标。

    2.tabBarIcon是一个提供 focused, color, size参数的函数。
    3.tabBarActiveTintColor和tabBarInactiveTintColor,可以在这里更改它们颜色。传递给tabBarIcon的颜色是活动的还是非活动的,这取决于聚焦状态(聚焦是活动的)。size是标签栏所期望的图标大小。

    给icons添加徽章

    有时我们想在一些图标上添加徽章。你可以使用tabBarBadge选项:

    { tabBarBadge: 3 }} />
    
    • 1

    tabs之间跳转

    从一个tab跳转到另一个tab,也是使用 navigation.navigate

    function HomeScreen({ navigation }) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Home!</Text>
          <Button
            title="Go to Settings"
            onPress={() => navigation.navigate('Settings')}
          />
        </View>
      );
    }
    
    function SettingsScreen({ navigation }) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Settings!</Text>
          <Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
        </View>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    每个tab的堆栈导航器

    每个标签中都有独立的导航栈

    import * as React from 'react';
    import { Button, Text, View } from 'react-native';
    import { NavigationContainer } from '@react-navigation/native';
    import { createNativeStackNavigator } from '@react-navigation/native-stack';
    import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    
    function DetailsScreen() {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Details!</Text>
        </View>
      );
    }
    
    function HomeScreen({ navigation }) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Home screen</Text>
          <Button
            title="Go to Details"
            onPress={() => navigation.navigate('Details')}
          />
        </View>
      );
    }
    
    function SettingsScreen({ navigation }) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text>Settings screen</Text>
          <Button
            title="Go to Details"
            onPress={() => navigation.navigate('Details')}
          />
        </View>
      );
    }
    
    const HomeStack = createNativeStackNavigator();
    
    function HomeStackScreen() {
      return (
        <HomeStack.Navigator>
          <HomeStack.Screen name="Home" component={HomeScreen} />
          <HomeStack.Screen name="Details" component={DetailsScreen} />
        </HomeStack.Navigator>
      );
    }
    
    const SettingsStack = createNativeStackNavigator();
    
    function SettingsStackScreen() {
      return (
        <SettingsStack.Navigator>
          <SettingsStack.Screen name="Settings" component={SettingsScreen} />
          <SettingsStack.Screen name="Details" component={DetailsScreen} />
        </SettingsStack.Navigator>
      );
    }
    
    const Tab = createBottomTabNavigator();
    
    export default function App() {
      return (
        <NavigationContainer>
          <Tab.Navigator>
            <Tab.Screen name="Home" component={HomeStackScreen} />
            <Tab.Screen name="Settings" component={SettingsStackScreen} />
          </Tab.Navigator>
        </NavigationContainer>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    Drawer navigation

    导航中的常见模式是从左边(有时是右边)使用抽屉在屏幕之间导航。

    安装

    npm install @react-navigation/drawer
    yarn add @react-navigation/drawer
    
    • 1
    • 2

    也需要安装react-native-gesture-handler and react-native-reanimated.

    npm install react-native-gesture-handler react-native-reanimated
    或
    yarn add react-native-gesture-handler react-native-reanimated
    
    • 1
    • 2
    • 3

    在index.js或App.js中引入文件

    import 'react-native-gesture-handler';
    
    • 1

    基于drawer的实例

    import * as React from 'react';
    import { Button, View } from 'react-native';
    import { createDrawerNavigator } from '@react-navigation/drawer';
    import { NavigationContainer } from '@react-navigation/native';
    
    function HomeScreen({ navigation }) {
      return (
        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
          <Button
            onPress={() => navigation.navigate('Notifications')}
            title="Go to notifications"
          />
        </View>
      );
    }
    
    function NotificationsScreen({ navigation }) {
      return (
        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
          <Button onPress={() => navigation.goBack()} title="Go back home" />
        </View>
      );
    }
    
    const Drawer = createDrawerNavigator();
    
    export default function App() {
      return (
        <NavigationContainer>
          <Drawer.Navigator initialRouteName="Home">
            <Drawer.Screen name="Home" component={HomeScreen} />
            <Drawer.Screen name="Notifications" component={NotificationsScreen} />
          </Drawer.Navigator>
        </NavigationContainer>
      );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    打开和关闭drawer

    navigation.openDrawer();
    navigation.closeDrawer();
    
    • 1
    • 2

    切换drawer

    navigation.toggleDrawer();
    
    • 1
  • 相关阅读:
    MySQL死锁举例及代码如何解决
    Sass 使用
    乘风而起!企业级应用软件市场迅猛发展,有哪些机会可以把握?
    正则表达式 包含一些 但不包括 的命令
    安卓系统--翻译手机rom语言 添加多国语言 编译apk 反编译ODEX 工具步骤解析
    【MySQL】事务 详解
    ADC、DMA以及串口之间的联系和区别?
    Springboot整合Redis
    mssql拿shell
    FireFox禁用HTTP2
  • 原文地址:https://blog.csdn.net/wxl1390/article/details/126246224