react-navigation是react官方推荐的新路由。主要是为了原路由内存及卡顿的问题。react
react-navigation目标分为三种导航:git
StackNavigator相似顶部导航条,用来跳转页面和传递参数。
TabNavigator相似底部标签栏,用来区分模块。
DrawerNavigator抽屉,相似从App左侧滑出一个页面。github
StackNavigatorreact-native
navigationOptions:配置StackNavigator的一些属性。浏览器
title:标题,若是设置了这个导航栏和标签栏的title就会变成同样的,因此不推荐使用这个方法。
header:能够设置一些导航的属性,固然若是想隐藏顶部导航条只要将这个属性设置为null就能够了。
headerTitle:设置导航栏标题,推荐用这个方法。
headerBackTitle:设置跳转页面左侧返回箭头后面的文字,默认是上一个页面的标题。能够自定义,也能够设置为null
headerTruncatedBackTitle:设置当上个页面标题不符合返回箭头后的文字时,默认改为"返回"。(上个页面的标题过长,致使显示不下,因此改为了短一些的。)
headerRight:设置导航条右侧。能够是按钮或者其余。
headerLeft:设置导航条左侧。能够是按钮或者其余。
headerStyle:设置导航条的样式。背景色,宽高等。若是想去掉安卓导航条底部阴影能够添加elevation: 0,iOS下用shadowOpacity: 0。
headerTitleStyle:设置导航条文字样式。安卓上若是要设置文字居中,只要添加alignSelf:'center'就能够了。在安卓上会遇到,若是左边有返回箭头致使文字仍是没有居中的问题,最简单的解决思路就是在右边也放置一个空的按钮。
headerBackTitleStyle:设置导航条返回文字样式。
headerTintColor:设置导航栏文字颜色。总感受和上面重叠了。
headerPressColorAndroid:安卓独有的设置颜色纹理,须要安卓版本大于5.0
gesturesEnabled:是否支持滑动返回手势,iOS默认支持,安卓默认关闭
gestureResponseDistance:对象覆盖触摸从屏幕边缘开始的距离,以识别手势。 它须要如下属性:
horizontal - number - 水平方向的距离 默认为25。
vertical - number - 垂直方向的距离 默认为135。app
效果动画
mode:定义跳转风格。ui
card:使用iOS和安卓默认的风格。
modal:iOS独有的使屏幕从底部画出。相似iOS的present效果
headerMode:边缘滑动返回上级页面时动画效果。this
float:iOS默认的效果,能够看到一个明显的过渡动画。
screen:滑动过程当中,整个页面都会返回。
none:没有动画。
cardStyle:自定义设置跳转效果。url
transitionConfig: 自定义设置滑动返回的配置。
onTransitionStart:当转换动画即将开始时被调用的功能。
onTransitionEnd:当转换动画完成,将被调用的功能。
path:路由中设置的路径的覆盖映射配置。
initialRouteName:设置默认的页面组件,必须是上面已注册的页面组件。
initialRouteParams:初始路由的参数。
path:path属性适用于其余app或浏览器使用url打开本app并进入指定页面。path属性用于声明一个界面路径。
新建一个ChatScreen.js文件。navigationOptions属性为导航的基本信息,能够包括标题等。
import React from 'react'; import { Text, View, Button } from 'react-native'; export default class ChatScreen extends React.Component { static navigationOptions = ({ navigation }) => ({ title: `Chat with ${navigation.state.params.user}`, }); render() { const { params } = this.props.navigation.state; return ( <View> <Text>Chat with {params.user}</Text> </View> ); } }
新建HomeScreen.js文件。
import React from 'react'; import { AppRegistry, Text, View, Button } from 'react-native'; export default class HomeScreen extends React.Component { static navigationOptions = { title: 'Welcome',//在导航中显示的标题内容 }; render() { const { navigate } = this.props.navigation; return ( <View> <Text>Hello, Chat App!</Text> <Button onPress={() => navigate('Chat',{user:'111'})} title="Chat with Lucy" /> </View> ); } }
新建App.js文件添加这两个页面到路由
import React from 'react'; import { StackNavigator } from 'react-navigation'; import HomeScreen from './HomeScreen' import ChatScreen from './ChatScreen' const SimpleApp = StackNavigator({ Home: { screen: HomeScreen }, Chat: { screen: ChatScreen }, }); export default SimpleApp;
TabNavigator
import React from 'react'; import { StyleSheet, Button } from 'react-native' import { TabNavigator } from 'react-navigation'; class MyHomeScreen extends React.Component { static navigationOptions = { tabBarLabel: 'Home', // Note: By default the icon is only shown on iOS. Search the showIcon option below. tabBarIcon: ({ tintColor }) => ( <Image source={require('./image/img_74.png')} style={[styles.icon, {tintColor: tintColor}]} /> ), }; render() { return ( <Button onPress={() => this.props.navigation.navigate('Notifications')} title="Go to notifications" /> ); } } class MyNotificationsScreen extends React.Component { static navigationOptions = { tabBarLabel: 'Notifications', tabBarIcon: ({ tintColor }) => ( <Image source={require('./image/img_74.png')} style={[styles.icon, {tintColor: tintColor}]} /> ), }; render() { return ( <Button onPress={() => this.props.navigation.goBack()} title="Go back home" /> ); } } const styles = StyleSheet.create({ icon: { width: 26, height: 26, }, }); const MyApp = TabNavigator({ Home: { screen: MyHomeScreen, }, Notifications: { screen: MyNotificationsScreen, }, }, { tabBarPosition: 'bottom', animationEnabled: true, tabBarOptions: { activeTintColor: '#e91e63', }, }); export default MyApp;
DrawerNavigator
import React from 'react'; import { StyleSheet, Button, Image } from 'react-native' import { DrawerNavigator } from 'react-navigation'; class MyHomeScreen extends React.Component { static navigationOptions = { drawerLabel: 'Home', drawerIcon: ({ tintColor }) => ( <Image source={require('./image/img_74.png')} style={[styles.icon, {tintColor: tintColor}]} /> ), }; render() { return ( <Button onPress={() => this.props.navigation.navigate('Notifications')} title="Go to notifications" /> ); } } class MyNotificationsScreen extends React.Component { static navigationOptions = { drawerLabel: 'Notifications', drawerIcon: ({ tintColor }) => ( <Image source={require('./image/img_74.png')} style={[styles.icon, {tintColor: tintColor}]} /> ), }; render() { return ( <Button onPress={() => this.props.navigation.goBack()} title="Go back home" /> ); } } const styles = StyleSheet.create({ icon: { width: 24, height: 24, }, }); const MyApp = DrawerNavigator({ Home: { screen: MyHomeScreen, }, Notifications: { screen: MyNotificationsScreen, }, }); export default MyApp;