导航器
官网: reactnavigation.org/
yarn add @react-navigation/native
yarn add react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
npx pod-install ios
cd ios
pod install
具体导航
yarn add react-navigation
yarn add react-navigation-stack
yarn add react-navigation-tabs
cd ios
pod install
顶部导航
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import Home from './src/Home'
import About from './src/About'
var RootStack = createStackNavigator(
{
Home: {
screen: Home,
navigationOptions: {
title: '首页',
headerBackTitle: '自定义返回标题',
headerStyle: {
backgroundColor: 'green'
},
headerTintColor: '#fff' ,
headerTitleStyle: {
fontSize: 20
},
headerLift: () => {
return (
<Button title='设置' />
)
}
}
},
About: {
screen: About,
navigationOptions: {
headerBackTitle: '自定义返回标题',
}
}
},
{
initialRouteName: 'Home',
defaultNavigationOptions: {
headerStyle: {
backgroundColor: 'yellow',
},
headerTitleStyle: {
color: 'whit'
}
}
}
);
RootStack.navigationOptions = ({navigation}) => {
let tabBarVisible = true
if(navigation.state.index > 0){
tabBarVisible = false
}
return tabBarVisible;
}
class App extends Component {
render() {
RootStack = createAppContainer(RootStack)
return <RootStack />
}
}
页面跳转(传参)
<Button title='跳转详情页' onPress={()=>{
this.props.navigation.navigate('About',{
id: 1,
name: '这是传递的数据'
})
this.props.navigation.goBack()
this.props.navigation.popToTop
}}
/>
接受参数和自定义导航标题
class About extends Component {
static navigationOptions = (props) => {
const { navigation } = props
const { state: { id }} = navigation
return {
title: '页面内设置标题',
headerRight: (
<Button title='保存' onPress={() => {
navigation.navigate('Cart') // 页面间跳转
}} />
)
}
}
render() {
return (
<View>
<Text>About</Text>
</View>
)
}
}
底部导航
import { createAppContainer } from 'react-navigation'
import { createBottomTabNavigator } from 'react-navigation-tabs'
import Icon from 'react-native-vector-icons/FontAwesome'
var BottomTabNavigators = createBottomTabNavigator(
{
Home: {
screen: About,
navigationOptions:{
title: '首页'
}
},
About: {
screen: Home,
navigationOptions:{
tabBarLabel: '关于'
}
},
Cart: {
screen: Cart,
navigationOptions:{
title: '购物车'
}
}
},
{
trueinitialRouteName: Home,
lazy: true ,
order: ['Cart','Home','About'],
initialRouteName: 'Home',
defaultNavigationOptions:() => ({
tabBarIcon: ({focused, tintColor, horizontal}) => {
const { routeName } = navigation.state
let iconName
if(iconName == "Home"){
iconName = 'envelope-o'
}else if(iconName == "About"){
iconName = 'user'
}else{
iconName = 'cog'
}
return <Icon name={iconName} size={20} color={tintColor} />
}
}),
animationEnabled: false,
tabBarPosition: 'bottom',
swipeEnabled: true,
backBehavior: 'none',
tabBarOptions: {
activeTintColor: '#47aaff',
activeBackgroundColor: 活动选项卡的背景色,
inactiveTintColor: '#000',
inactiveBackgroundColor: 非活动选项卡的背景色,
showIcon: true,
showLabel: false,
indicatorStyle: {
height: 0
},
style: {
backgroundColor: '#fff',
height: 50,
},
labelStyle: {
fontSize: 12,
margin:0,
},
},
pressColor:'material',
}
)
class App extends Component {
render() {
RootStack = createAppContainer(BottomTabNavigators);
return <RootStack />
}
}
欢迎页
import { createAppContainer, createSwitchNavigator } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
const AppInitNavigator = createStackNavigator({
welcome: {
screen: WelcomePage ,
navigationOptions: {
header: null
}
}
})
const switchNavigator = createSwitchNavigator({
Init: AppInitNavigator,
Main: BottomTabNavigators
})
class App extends Component {
render() {
RootStack = createAppContainer(switchNavigator);
return <RootStack />
}
}