内容简介:从createDrawerNavigator API上可以看出
createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig):
RouteConfigs DrawerNavigatorConfig
从createDrawerNavigator API上可以看出 createDrawerNavigator
支持通过 RouteConfigs
和 DrawerNavigatorConfig
两个参数来创建createDrawerNavigator导航器。
RouteConfigs支持三个参数 screen
、 path
以及 navigationOptions
;
-
screen
(必选):指定一个 React 组件作为屏幕的主要显示内容,当这个组件被DrawerNavigator加载时,它会被分配一个navigation
prop。 -
path
(可选):用来设置支持schema跳转时使用,具体使用会在下文的有关Schema
章节中讲到; -
navigationOptions
(可选):用以配置全局的屏幕导航选项如:title、headerRight、headerLeft等;
-
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。
DrawerNavigator有个默认的带滚动的侧边栏,你也可以通过重写这个侧边栏组件来自定义侧边栏:
contentComponent:(props) => ( <ScrollView style={{backgroundColor:'#987656',flex:1}}> <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}> <DrawerItems {...props} /> </SafeAreaView> </ScrollView> ) 复制代码
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的视频教程,欢迎学习。
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
对应切换侧边栏操作,要进行这些操作我么还需要一个 navigation
, navigation
可以从props中获取;
navigation.openDrawer(); navigation.closeDrawer(); navigation.toggleDrawer();
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的视频教程,欢迎学习。
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> } 复制代码
代码解析:
页面跳转可分为两步:
-
- 获取navigation:
const {navigation} = this.props; 复制代码
-
- 通过
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开发的更多实战经验和技巧,以及优化思路。
以上所述就是小编给大家介绍的《『React Navigation 3x系列教程』createDrawerNavigator开发指南》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python编程
[美]埃里克·马瑟斯 / 袁国忠 / 人民邮电出版社 / 2016-7-1 / CNY 89.00
本书是一本针对所有层次的Python 读者而作的Python 入门书。全书分两部分:第一部分介绍用Python 编程所必须了解的基本概念,包括matplotlib、NumPy 和Pygal 等强大的Python 库和工具介绍,以及列表、字典、if 语句、类、文件与异常、代码测试等内容;第二部分将理论付诸实践,讲解如何开发三个项目,包括简单的Python 2D 游戏开发如何利用数据生成交互式的信息图......一起来看看 《Python编程》 这本书的介绍吧!