createDrawerNavigator抽屉效果,侧边滑出:html
createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig):
RouteConfigs
(必选):路由配置对象是从路由名称到路由配置的映射,告诉导航器该路由呈现什么。DrawerNavigatorConfig
(可选):配置导航器的路由(如:默认首屏,navigationOptions,paths等)样式(如,转场模式mode、头部模式等)。从createDrawerNavigator API上能够看出createDrawerNavigator
支持经过RouteConfigs
和 DrawerNavigatorConfig
两个参数来建立createDrawerNavigator导航器。react
RouteConfigs支持三个参数screen
、path
以及navigationOptions
;react-native
screen
(必选):指定一个 React 组件做为屏幕的主要显示内容,当这个组件被DrawerNavigator加载时,它会被分配一个navigation
prop。path
(可选):用来设置支持schema跳转时使用,具体使用会在下文的有关Schema
章节中讲到;navigationOptions
(可选):用以配置全局的屏幕导航选项如:title、headerRight、headerLeft等;DrawerNavigator有个默认的带滚动的侧边栏,你也能够经过重写这个侧边栏组件来自定义侧边栏:数组
contentComponent:(props) => ( <ScrollView style=> <SafeAreaView forceInset=> <DrawerItems {...props} /> </SafeAreaView> </ScrollView> )
contentOptions主要配置侧滑栏item的属性,只对DrawerItems,例如咱们刚才写的例子,就能够经过这个属性来配置颜色,背景色等。其主要属性有:函数
contentOptions: { activeTintColor: '#e91e63', itemsContainerStyle: { marginVertical: 0, }, iconContainerStyle: { opacity: 1 } }
DrawerNavigator支持的屏幕导航选项的参数有:学习
title: 能够用做headerTitle和drawerLabel的备选的通用标题。动画
drawerLabel:侧滑标题;this
drawerIcon:侧滑的标题图标,这里会回传两个参数:spa
{focused: boolean, tintColor: string}
:code
drawerLockMode:指定抽屉的锁定模式。 这也能够经过在顶级路由器上使用screenProps.drawerLockMode 动态更新。
侧边栏支持如下几种操做方式:
navigation.openDrawer(); navigation.closeDrawer(); navigation.toggleDrawer(); //或 navigation.dispatch(DrawerActions.openDrawer()); navigation.dispatch(DrawerActions.closeDrawer()); navigation.dispatch(DrawerActions.toggleDrawer());
其中路由名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=/> ), } }, Page5: { screen: Page5, navigationOptions: { drawerLabel: 'Page5', drawerIcon: ({tintColor}) => ( <MaterialIcons name="move-to-inbox" size={24} style= /> ), } }, }, { initialRouteName: 'Page4', contentOptions: { activeTintColor: '#e91e63', }, contentComponent:(props) => ( <ScrollView style=> <SafeAreaView forceInset=> <DrawerItems {...props} /> </SafeAreaView> </ScrollView> ) } );
DrawerNavigator的navigationOptions有两个关键的属性,tabBarLabel标签与tabBarIcon图标:
Page4: { screen: Page4, navigationOptions: { drawerLabel: 'Page4', drawerIcon: ({tintColor}) => ( <MaterialIcons name="drafts" size={24} style=/> ), } },
在上述代码中使用了react-native-vector-icons
的矢量图标做为Tab的显示图标,drawerIcon接收一个React 组件,你们能够根据须要进行定制:
render() { const {navigation} = this.props; return <View style=> <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> }
代码解析:
页面跳转可分为两步:
const {navigation} = this.props;
navigate(routeName, params, action)
进行页面跳转:navigation.navigate('Page5'); });
若是DrawerNavigator的侧边栏的效果没法知足咱们的需求,咱们能够经过contentComponent
属性来自定义侧边栏:
contentComponent:(props) => ( <ScrollView style=> <SafeAreaView forceInset=> <DrawerItems {...props} /> </SafeAreaView> </ScrollView> )