createBottomTabNavigator
至关于iOS里面的TabBarController,屏幕下方的标签栏。如图: html
createBottomTabNavigator(RouteConfigs, BottomTabNavigatorConfig):
react
RouteConfigs
(必选):路由配置对象是从路由名称到路由配置的映射,告诉导航器该路由呈现什么。BottomTabNavigatorConfig
(可选):配置导航器的路由(如:默认首屏,navigationOptions,paths等)样式(如,转场模式mode、头部模式等)。从createBottomTabNavigator API上能够看出createBottomTabNavigator
支持经过RouteConfigs
和 BottomTabNavigatorConfig
两个参数来建立createBottomTabNavigator导航器。ios
RouteConfigs支持三个参数screen
、path
以及navigationOptions
;react-native
screen
(必选):指定一个 React 组件做为屏幕的主要显示内容,当这个组件被TabNavigator加载时,它会被分配一个navigation
prop。path
(可选):用来设置支持schema跳转时使用,具体使用会在下文的有关Schema
章节中讲到;navigationOptions
(可选):用以配置全局的屏幕导航选项如:title、headerRight、headerLeft等;tabBarComponent:指定createBottomTabNavigator的TabBar组件,若是不指定在iOS上默认使用TabBarBottom,在Android平台上默认使用TabBarTop数组
TabBarBottom
与TabBarTop
都是react-navigation
所支持的组件,要自定义TabBar能够重写这两个组件也能够根据须要本身实现一个;tabBarOptions: 配置TaBar下文会详细讲解;函数
initialRouteName : 默认页面组件,createBottomTabNavigator显示的第一个页面;学习
order: 定义tab顺序的routeNames数组。测试
paths: 提供routeName到path config的映射,它覆盖routeConfigs中设置的路径。字体
backBehavior: 后退按钮是否会致使标签切换到初始tab? 若是是,则设切换到初始tab,不然什么也不作。 默认为切换到初始tab。this
tabBarOptions: { labelStyle: { fontSize: 12, }, tabStyle: { width: 100, }, style: { backgroundColor: 'blue', }, }
createBottomTabNavigator支持的屏幕导航选项的参数有:
title: 能够用做headerTitle和tabBarLabel的备选的通用标题。
tabBarVisible: 显示或隐藏TabBar,默认显示;
tabBarIcon: 设置TabBar的图标;
tabBarLabel: 设置TabBar的标签;
tabBarOnPress: Tab被点击的回调函数,它的参数是一保函一下变量的对象:
tabBarButtonComponent:React组件,它包装图标和标签并实现onPress。 默认状况下是TouchableWithoutFeedback的一个封装,使其其表现与其它可点击组件相同,tabBarButtonComponent: TouchableOpacity 将使用 TouchableOpacity 来替代;
tabBarAccessibilityLabel:选项卡按钮的辅助功能标签。 当用户点击标签时,屏幕阅读器会读取这些信息。 若是您没有选项卡的标签,建议设置此项;
tabBarTestID:用于在测试中找到该选项卡按钮的 ID;
export const AppTabNavigator = createBottomTabNavigator({ Page1: { screen: Page1, navigationOptions: { tabBarLabel: 'Page1', tabBarIcon: ({tintColor, focused}) => ( <Ionicons name={focused ? 'ios-home' : 'ios-home-outline'} size={26} style= /> ), } }, Page2: { screen: Page2, navigationOptions: { tabBarLabel: 'Page2', tabBarIcon: ({tintColor, focused}) => ( <Ionicons name={focused ? 'ios-people' : 'ios-people-outline'} size={26} style= /> ), } }, Page3: { screen: Page3, navigationOptions: { tabBarLabel: 'Page3', tabBarIcon: ({tintColor, focused}) => ( <Ionicons name={focused ? 'ios-chatboxes' : 'ios-chatboxes-outline'} size={26} style= /> ), } }, }, { tabBarComponent: TabBarComponent, tabBarOptions: { activeTintColor: Platform.OS === 'ios' ? '#e91e63' : '#fff', } });
TabNavigator的navigationOptions有两个关键的属性,tabBarLabel标签与tabBarIcon图标:
Page2: { screen: Page2, navigationOptions: { tabBarLabel: 'Page2', tabBarIcon: ({tintColor, focused}) => ( <Ionicons name={focused ? 'ios-people' : 'ios-people-outline'} size={26} style= /> ), } },
在上述代码中使用了react-native-vector-icons
的矢量图标做为Tab的显示图标,tabBarIcon接收一个React 组件,你们能够根据须要进行定制:
const {navigation} = this.props; ... <Button title="跳转到页面2" onPress={() => { navigation.navigate("Page3",{ name: 'Devio' }) }} /> <Button title="Go Back" onPress={() => { navigation.goBack(); }} />
页面跳转可分为两步:
const {navigation} = this.props;
navigate(routeName, params, action)
进行页面跳转:navigation.navigate('Page2'); navigation.navigate('Page3',{ name: 'Devio' });
这里在跳转到Page3
的时候传递了参数{ name: 'Devio' }
;
export default class Page1 extends React.Component { //也可在这里定义每一个页面的导航属性,这里的定义会覆盖掉别处的定义 // static navigationOptions = { // title: 'Page1', // }; render() { const {navigation} = this.props; return <View style=> <Text style={styles.text}>欢迎来到Page1</Text> <Button title="Go Back" onPress={() => { navigation.goBack(); }} /> <Button title="改变主题色" onPress={() => { navigation.setParams({theme:{ tintColor:'orange', updateTime:new Date().getTime() }}) }} /> <Button title="跳转到页面2" onPress={() => { navigation.navigate("Page2") }} /> </View> } }
代码解析:
在上述代码中经过:
<Button title="改变主题色" onPress={() => { navigation.setParams({theme:{ tintColor:'orange', updateTime:new Date().getTime() }}) }} />
更新当前主题,你会看到当点击“改变主题色“按钮时,TabBar的颜色也会跟着改变。
当用户单击Go Back
按钮时,经过:
navigation.goBack();
实现了返回到默认的Tab。
在使用react-navigation时每每有些需求经过简单的配置是没法完成的,好比: