這篇文章將向你們分享createDrawerNavigator的一些開發指南和實用技巧。html
createDrawerNavigator
抽屜效果,側邊滑出:react
createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig):
react-native
RouteConfigs
(必選):路由配置對象是從路由名稱到路由配置的映射,告訴導航器該路由呈現什麼。DrawerNavigatorConfig
(可選):配置導航器的路由(如:默認首屏,navigationOptions,paths等)樣式(如,轉場模式mode、頭部模式等)。從createDrawerNavigator API上能夠看出createDrawerNavigator
支持經過RouteConfigs
和 DrawerNavigatorConfig
兩個參數來建立createDrawerNavigator導航器。數組
RouteConfigs支持三個參數screen
、path
以及navigationOptions
;bash
screen
(必選):指定一個 React 組件做爲屏幕的主要顯示內容,當這個組件被DrawerNavigator加載時,它會被分配一個navigation
prop。path
(可選):用來設置支持schema跳轉時使用,具體使用會在下文的有關Schema
章節中講到;navigationOptions
(可選):用以配置全局的屏幕導航選項如:title、headerRight、headerLeft等;drawerWidth: 設置側邊菜單的寬度;函數
drawerPosition: 設置側邊菜單的位置,支持'left'、 'right',默認是'left';學習
contentComponent: 用於呈現抽屜導航器內容的組件,例如導航項。接收抽屜導航器的 navigation 屬性 。默認爲DrawerItems。有關詳細信息,請參閱下文;flex
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,例如咱們剛纔寫的例子,就能夠經過這個屬性來配置顏色,背景色等。其主要屬性有:
eg:
contentOptions: {
activeTintColor: '#e91e63',
itemsContainerStyle: {
marginVertical: 0,
},
iconContainerStyle: {
opacity: 1
}
}
複製代碼
DrawerNavigator支持的屏幕導航選項的參數有:
{focused: boolean, tintColor: string}
:
側邊欄支持如下幾種操做方式:
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={{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>
)
}
);
複製代碼
DrawerNavigator的navigationOptions有兩個關鍵的屬性,tabBarLabel標籤與tabBarIcon圖標:
Page4: {
screen: Page4,
navigationOptions: {
drawerLabel: 'Page4',
drawerIcon: ({tintColor}) => (
<MaterialIcons name="drafts" size={24} style={{color: tintColor}}/>
),
}
},
複製代碼
在上述代碼中使用了react-native-vector-icons
的矢量圖標做爲Tab的顯示圖標,drawerIcon接收一個React 組件,你們能夠根據須要進行定製:
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>
}
複製代碼
代碼解析:
頁面跳轉可分爲兩步:
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>
)
複製代碼