『React Navigation 3x系列教程』createDrawerNavigator开发指南

news/2024/7/8 12:17:49

这篇文章将向大家分享createDrawerNavigator的一些开发指南和实用技巧。

createDrawerNavigator抽屉效果,侧边滑出:

createDrawerNavigator API

createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig):

  • RouteConfigs(必选):路由配置对象是从路由名称到路由配置的映射,告诉导航器该路由呈现什么。
  • DrawerNavigatorConfig(可选):配置导航器的路由(如:默认首屏,navigationOptions,paths等)样式(如,转场模式mode、头部模式等)。

从createDrawerNavigator API上可以看出createDrawerNavigator支持通过RouteConfigsDrawerNavigatorConfig两个参数来创建createDrawerNavigator导航器。

RouteConfigs

RouteConfigs支持三个参数screenpath以及navigationOptions

  • screen(必选):指定一个 React 组件作为屏幕的主要显示内容,当这个组件被DrawerNavigator加载时,它会被分配一个navigation prop。
  • path(可选):用来设置支持schema跳转时使用,具体使用会在下文的有关Schema章节中讲到;
  • navigationOptions(可选):用以配置全局的屏幕导航选项如:title、headerRight、headerLeft等;

DrawerNavigatorConfig

  • drawerWidth: 设置侧边菜单的宽度;

  • drawerPosition: 设置侧边菜单的位置,支持'left'、 'right',默认是'left';

  • contentComponent: 用于呈现抽屉导航器内容的组件,例如导航项。接收抽屉导航器的 navigation 属性 。默认为DrawerItems。有关详细信息,请参阅下文;

  • contentOptions: 配置抽屉导航器内容,见下文;

  • useNativeAnimations: 是否启用Native动画,默认启用;

  • drawerBackgroundColor: 侧边菜单的背景;

  • initialRouteName: 初始化哪个界面为根界面,如果不配置,默认使用RouteConfigs中的第一个页面当做根界面;

  • order: drawer排序,默认使用配置路由的顺序;

  • paths: 提供routeName到path config的映射,它覆盖routeConfigs中设置的路径。

  • backBehavior: 后退按钮是否会导致标签切换到初始drawer? 如果是,则设切换到初始drawer,否则什么也不做。 默认为切换到初始drawer。

自定义侧边栏(contentComponent)

DrawerNavigator有个默认的带滚动的侧边栏,你也可以通过重写这个侧边栏组件来自定义侧边栏:

contentComponent:(props) => (
    <ScrollView style={{backgroundColor:'#987656',flex:1}}>
        <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
            <DrawerItems {...props} />
        </SafeAreaView>
    </ScrollView>
)
复制代码

DrawerItems的contentOptions

contentOptions主要配置侧滑栏item的属性,只对DrawerItems,例如我们刚才写的例子,就可以通过这个属性来配置颜色,背景色等。其主要属性有:

  • items: 路由数组,如果要修改路由可以可以修改或覆盖它;
  • activeItemKey: 定义当前选中的页面的key;
  • activeTintColor: 选中item状态的文字颜色;
  • activeBackgroundColor: 选中item的背景色;
  • inactiveTintColor: 未选中item状态的文字颜色;
  • inactiveBackgroundColor: 未选中item的背景色;
  • onItemPress: 选中item的回调,这个参数属性为函数,会将当前路由回调过去;
  • itemsContainerStyle: 定义itemitem容器的样式;
  • itemStyle: 定义item的样式;
  • labelStyle: 定义item文字的样式;
  • iconContainerStyle: 定义item图标容器的样式;
  • activeLabelStyle:选中状态下文本样式;
  • inactiveLabelStyle:非选中状态下文本样式;
  • iconContainerStyle :用于设置图标容器的样式。

eg:

contentOptions: {
  activeTintColor: '#e91e63',
  itemsContainerStyle: {
    marginVertical: 0,
  },
  iconContainerStyle: {
    opacity: 1
  }
}
复制代码

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

navigationOptions(屏幕导航选项)

DrawerNavigator支持的屏幕导航选项的参数有:

  • title: 可以用作headerTitle和drawerLabel的备选的通用标题。
  • drawerLabel:侧滑标题;
  • drawerIcon:侧滑的标题图标,这里会回传两个参数:
    • {focused: boolean, tintColor: string}
      • focused: 表示是否是选中状态;
      • tintColor: 表示选中的颜色;
  • drawerLockMode:指定抽屉的锁定模式。 这也可以通过在顶级路由器上使用screenProps.drawerLockMode 动态更新。

侧边栏操作(打开、关闭、切换)

侧边栏支持以下几种操作方式:

navigation.openDrawer();
navigation.closeDrawer();
navigation.toggleDrawer();
//或
navigation.dispatch(DrawerActions.openDrawer());
navigation.dispatch(DrawerActions.closeDrawer());
navigation.dispatch(DrawerActions.toggleDrawer());
复制代码

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

其中路由名openDrawer对应这打开侧边栏的操作,DrawerClose对应关闭侧边栏的操作,toggleDrawer对应切换侧边栏操作,要进行这些操作我么还需要一个navigationnavigation可以从props中获取;

  • 打开侧边栏:navigation.openDrawer();
  • 关闭侧边栏:navigation.closeDrawer();
  • 切换侧边栏:navigation.toggleDrawer();

其他API

【案例1】使用DrawerNavigator做界面导航、配置navigationOptions、自定义侧边栏

第一步:创建一个createDrawerNavigator类型的导航器

export const DrawerNav = createDrawerNavigator({
        Page4: {
            screen: Page4,
            navigationOptions: {
                drawerLabel: 'Page4',
                drawerIcon: ({tintColor}) => (
                    <MaterialIcons name="drafts" size={24} style={{color: tintColor}}/>
                ),
            }
        },
        Page5: {
            screen: Page5,
            navigationOptions: {
                drawerLabel: 'Page5',
                drawerIcon: ({tintColor}) => (
                    <MaterialIcons
                        name="move-to-inbox"
                        size={24}
                        style={{color: tintColor}}
                    />
                ),
            }
        },
    },
    {
        initialRouteName: 'Page4',
        contentOptions: {
            activeTintColor: '#e91e63',
        },
        contentComponent:(props) => (
            <ScrollView style={{backgroundColor:'#987656',flex:1}}>
                <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
                    <DrawerItems {...props} />
                </SafeAreaView>
            </ScrollView>
        )
    }
);
复制代码

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

第二步:配置navigationOptions:

DrawerNavigator的navigationOptions有两个关键的属性,tabBarLabel标签与tabBarIcon图标:

Page4: {
    screen: Page4,
    navigationOptions: {
        drawerLabel: 'Page4',
        drawerIcon: ({tintColor}) => (
            <MaterialIcons name="drafts" size={24} style={{color: tintColor}}/>
        ),
    }
},
复制代码

提示:和本文配套的还有一个React Navigation3x的视频教程,欢迎学习。

在上述代码中使用了react-native-vector-icons的矢量图标作为Tab的显示图标,drawerIcon接收一个React 组件,大家可以根据需要进行定制:

  • tintColor: 当前状态下Item的颜色;
  • focused: Item是否被选中;

第三步:界面跳转

render() {
    const {navigation} = this.props;
    return <View style={{flex: 1, backgroundColor: "#f67888",}}>
        <Text style={styles.text}>欢迎来到Page5</Text>
        <Button
            onPress={() => navigation.openDrawer()}
            title="Open drawer"
        />
        <Button
            onPress={() => navigation.toggleDrawer()}
            title="Toggle drawer"
        />
        <Button
            onPress={() => navigation.navigate('Page4')}
            title="Go to Page4"
        />
    </View>
}
复制代码

代码解析:

页面跳转可分为两步:

    1. 获取navigation:
    const {navigation} = this.props;
    复制代码
    1. 通过navigate(routeName, params, action)进行页面跳转:
	 navigation.navigate('Page5');
 });
复制代码

自定义侧边栏

如果DrawerNavigator的侧边栏的效果无法满足我们的需求,我们可以通过contentComponent属性来自定义侧边栏:

contentComponent:(props) => (
    <ScrollView style={{backgroundColor:'#987656',flex:1}}>
        <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
            <DrawerItems {...props} />
        </SafeAreaView>
    </ScrollView>
)
复制代码
  • 大家在学习使用React Navigation3x过程中遇到任何问题都可以在React Navigation3x的视频教程中寻找答案哈。
  • 另外,也可以通过最新版React Native+Redux打造高质量上线App视频教程学习React Navigation开发的更多实战经验和技巧,以及优化思路。

http://www.niftyadmin.cn/n/3059533.html

相关文章

如何在内核中重新编译某一个ko模块

例如&#xff1a;SDIO 模块 1、用 lsmod 查看sdhci 相关的模块。 2、rmmod sdhci-pci 和 sdhci 3、make modules SUBDIRSdrivers/mmc 4、find. -name *.ko 查找生成的.ko文件 5、insmod sdhci-pci.ko 和sdhci.ko 这样可以只是编译drivers/mmc 目录下的ko模块

命令行方式查看windows下机器的配置

需要在C:/Program Files/Common Files/Microsoft Shared/MSInfo目录下执行Msinfo32 命令&#xff1a;C:/Program Files/Common Files/Microsoft Shared/MSInfo>Msinfo32 /report d:/cmpInfo.txt /categories SystemSummary进入d盘使用type查看生成的文件d:/> type cmp…

Linux内核编译过程

准备工作 硬件&#xff1a;笔记本 系统&#xff1a; Ubuntu18.04 64位 下载内核 1、先安装ubuntu18.04的系统。 2、到内核官网下载最新的内核code&#xff1a; https://www.kernel.org/ 如图所示下载最新kernel 例如 Linux-4.19&#xff1a; 3、将下载的内核 Linux-4.1…

Linux Shell nohup命令用法

在应用Unix/Linux时&#xff0c;我们一般想让某个程序在后台运行&#xff0c;于是我们将常会用 & 在程序结尾来让程序自动运行。比如我们要运行mysql在后台&#xff1a; /usr/local/mysql/bin/mysqld_safe –usermysql &。可是有很多程序并不想mysqld一样&#xff0c;这…

必应搜索昨日起出现大规模的无法访问

昨天下午&#xff0c;必应搜索在国内出现大规模的访问故障&#xff0c;显示“无法访问此网站”&#xff0c;具体原因未知。必应由微软公司在2009年推出&#xff0c;英文名为 Bing&#xff0c;是全球领先的搜索引擎之一。对昨天下午起国内出现的大规模访问故障&#xff0c;有网友…

python 正则匹配+爬虫+返回信息对比 实例(亲自测试)

①将输入两个文件的txt&#xff0c;正则匹配&#xff0c;进行拼接&#xff1a;&#xff08;所有的&#xff09;②将text信息&#xff08;“找一下恒洁”&#xff09;&#xff0c;组成完整的URL链接&#xff0c;进行爬虫搜索 ③保存所有的content&#xff0c;匹配自己所需要的信…

27 | 主库出问题了,从库怎么办?

在前面的第24、25和26篇文章中&#xff0c;我和你介绍了MySQL主备复制的基础结构&#xff0c;但这些都是一主一备的结构。 大多数的互联网应用场景都是读多写少&#xff0c;因此你负责的业务&#xff0c;在发展过程中很可能先会遇到读性能的问题。而在数据库层解决读性能问题&a…

Linux下硬盘读写压力测试脚本(Python版)

import os,time for i in range(300000):os.system(sudo time dd if/dev/sda4 of/test1.txt bs8k count300000)time.sleep(0.2)os.system(sudo rm /test1.txt)time.sleep(0.1)if i%10 0:print i os.system("echo END") if后面跟的是输入地址&#xff0c;使用lsblk命…