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> )