官网地址:https://reactnavigation.org
(1) StackNavigator: 类似于普通的Navigator,屏幕上方导航栏
(2) TabNavigator: 相当于iOS里面的TabBarController,屏幕下方的标签栏
(3) DrawerNavigator: 抽屉效果,侧边滑出
三个种类的Navigator功能强大,RN创建的app使用react-navigation,可以很好地搭建出页面架构。
首先先看看两个组件的api:
StackNavigator
API: StackNavigator(RouteConfigs, StackNavigatorConfig)
参数:RouteConfigs,StackNavigatorConfig
TabNavigator
API:TabNavigator(RouteConfigs, TabNavigatorConfig)
参数:RouteConfigs,TabNavigatorConfig
通常我们先创建TabNavigator,再将生成的TabNavigator给StackNavigator作为主页面:
//生成TabNavigator
let TabNav = TabNavigator(MainTabRoute, TabOpt)
//生成StackNavigator
let StackNav = StackNavigator({
//注册一个Root页面
Root: {
//TabNavigator作为Root的screen
screen: TabNav,
navigationOptions: {
header: false
}
},
...MainTabRoute, //这里通常配置一些Route,
...UserRoute, //可以配置多个
...OtherRoute,
}, StackOpt);
}
//TabOpt,StackOpt为相应的配置,具体字段查阅文档。
参数:
参数:
activeTintColor: '#ff8500', // 文字和图片选中颜色
inactiveTintColor: '#999', // 文字和图片未选中颜色
showIcon: true, // android 默认不显示 icon, 需要设置为 true 才会显示
indicatorStyle: {
height: 0 // 如TabBar下面显示有一条线,可以设高度为0后隐藏
},
style: {
backgroundColor: '#fff', // TabBar 背景色
height: 44
},
labelStyle: {
fontSize: 10, // 文字大小
},
DrawerNavigator
const DrawerNav = DrawerNavigator({
Home: { screen: Home },
Bill: { screen: Bill },
Me: { screen: Me },
HomeTwo: { screen: HomeTwo },
HomeThree: { screen: HomeThree },
HomeFour: { screen: HomeFour },
BillTwo: { screen: BillTwo },
BillThree: { screen: BillThree }
}, {
drawerWidth: 200, // 抽屉宽
drawerPosition: 'left', // 抽屉在左边还是右边
// contentComponent: CustomDrawerContentComponent, // 自定义抽屉组件
contentOptions: {
initialRouteName: Home, // 默认页面组件
activeTintColor: 'white', // 选中文字颜色
activeBackgroundColor: '#ff8500', // 选中背景颜色
inactiveTintColor: '#666', // 未选中文字颜色
inactiveBackgroundColor: '#fff', // 未选中背景颜色
style: { // 样式
}
}
});
在StackNavigator中注册后的组件都有navigation这个属性.
navigation又有5个方法:navigate, goBack, state, setParams, dispatch
navigate(routeName, params, action)
const {navigate} = this.props.navigation;
return (
This is the home screen of the app
)
const {state} = this.props.navigation; //拿到navigate(routeName, params, action)中的params参数
Name: {state.params.name}
const {setParams} = this.props.navigation;
const {goBack} = this.props.navigation;
//调用navigate的action
this.props.navigation.dispatch(navigateAction)
The following actions are supported:
'Home': {
screen: require('./Home').default,
path: 'Home',
navigationOptions: {
tabBarLabel: '首页',
tabBarIcon: (iconAttr) => crtIcon(iconAttr, 'home')
},
},
'Other': {
screen: require('./Other').default,
path: 'Other',
navigationOptions: {
tabBarLabel: '其他',
tabBarIcon: (iconAttr) => crtIcon(iconAttr, 'position')
},
},
'My': {
screen: require('./My').default,
path: 'My',
navigationOptions: {
tabBarLabel: '我的',
tabBarIcon: (iconAttr) => crtIcon(iconAttr, 'user')
},
},
'My/one': {
screen: require('./one').default,
path: 'My/one',
navigationOptions: {
header: false
},
},
'My/two': {
screen: require('./two').default,
path: 'My/two',
navigationOptions: {
title: '页面2'
},
},
'My/three': {
screen: require('./three').default,
path: 'My/three',
navigationOptions: {
title: '页面3'
},
},