问题
This is my actual Code:
App.js
import React from 'react';
import {StackNavigator} from 'react-navigation';
import DrawerStack from './src/stacks/drawerStack';
const Navigator = StackNavigator({
drawerStack: {screen: DrawerStack}
}, {
headerMode: 'screen',
initialRouteName: 'drawerStack'
})
export default Navigator
drawerStack.js
import React from 'react'
import {StackNavigator, DrawerActions} from "react-navigation";
import {Text, View, TouchableOpacity} from 'react-native';
import Home from "../components/home";
import DrawerScreen from "./drawerScreen";
const DrawerNavigation = StackNavigator({
DrawerStack: {screen: DrawerScreen}
}, {
headerMode: 'none',
// navigationOptions: ({navigation}) => ({
// headerStyle: {
// backgroundColor: 'rgb(255,45,85)',
// paddingLeft: 10,
// paddingRight: 10
// },
// title: 'Home',
// headerTintColor: 'white',
// headerLeft: <View>
// <TouchableOpacity
// onPress={() => {
// if (navigation.state.isDrawerOpen === false) {
// navigation.dispatch(DrawerActions.openDrawer());
// } else {
// navigation.dispatch(DrawerActions.closeDrawer());
// }
// }}>
// <Text>Menu</Text>
// </TouchableOpacity>
// </View>
// })
})
export default DrawerNavigation;
drawerScreen.js
import {DrawerNavigator} from 'react-navigation'
import Home from '../components/home';
import Login from '../components/login';
import Contacts from '../components/contacts';
import News from '../components/news';
const DrawerScreen = DrawerNavigator({
Home: {screen: Home},
Login: {screen: Login},
Contacts: {screen: Contacts},
News: {screen: News}
}, {
headerMode: 'screen',
initialRouteName: 'Home'
})
export default DrawerScreen;
news.js "example of a page where I want to manage a custom header"
import React from "react";
import {Text, View} from 'react-native';
export default class News extends React.Component {
static navigationOptions = {
headerTitle: 'News Header',
headerLeft: (
<Button
onPress={() => alert('This is an example button like hamburgher!')}
title="="
/>
)
};
render() {
return (
<View>
<Text> Here Leave the News!! </Text>
</View>
);
}
}
recapitulating the structure of the Navigator is this:
1) (app.js) -> StackNavigator
2) (DrawerStack.js) -> StackNavigator
3) (DrawerScreen.js) -> DrawerNavigator
what I can not understand is how to declare correctly
headerMode: 'screen'
in the various Navigator to show on certain pages (example News.js) a specific custom header.
回答1:
Change header style using navigationOptions
as below. You can change as your requirement. For more navigationOptions
see Configuring the header bar of React Navigation.
import {DrawerNavigator} from 'react-navigation'
import Home from '../components/home';
import Login from '../components/login';
import Contacts from '../components/contacts';
import News from '../components/news';
const DrawerScreen = DrawerNavigator({
Home: {screen: Home},
Login: {screen: Login},
Contacts: {screen: Contacts},
News: {
screen: News,
navigationOptions: {
title: "News",
headerTitleStyle: {
color: "white"
},
headerStyle: {
backgroundColor: "red"
},
headerBackTitle: null,
headerTintColor: "white"
}
}
},{
headerMode: 'screen',
initialRouteName: 'Home'
})
export default DrawerScreen;
来源:https://stackoverflow.com/questions/50757818/react-native-how-to-manage-headers-per-page-with-stacknavigator-and-drawernavi