这篇文章将向大家分享createDrawerNavigator的一些开发指南和实用技巧。
createDrawerNavigator
抽屉效果,侧边滑出:
createDrawerNavigator(RouteConfigs, DrawerNavigatorConfig):
RouteConfigs
(必选):路由配置对象是从路由名称到路由配置的映射,告诉导航器该路由呈现什么。DrawerNavigatorConfig
(可选):配置导航器的路由(如:默认首屏,navigationOptions,paths等)样式(如,转场模式mode、头部模式等)。从createDrawerNavigator API上可以看出createDrawerNavigator
支持通过RouteConfigs
和 DrawerNavigatorConfig
两个参数来创建createDrawerNavigator导航器。
RouteConfigs支持三个参数screen
、path
以及navigationOptions
;
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,例如我们刚才写的例子,就可以通过这个属性来配置颜色,背景色等。其主要属性有:
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=/>
),
}
},
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>
)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有