我有一个抽屉导航器,里面有 2 个堆栈。我希望每个堆栈的第一个屏幕在标题中显示汉堡包图标,但是当移动到堆栈中的第二个屏幕时,我希望将汉堡包替换为后退按钮。
如何将抽屉连接到此切换按钮并使其仅显示在堆栈中的第一个屏幕上?
在编辑堆栈中的第一个屏幕以包含切换按钮时,我没有得到任何响应。我遵循了详细的过程
将汉堡按钮添加到 React Native Navigation
这是我最好的尝试:
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { createStackNavigator, createDrawerNavigator, createAppContainer } from 'react-navigation';
/ --------------屏幕--------------- /
class FirstScreen extends React.Component {
static navigationOptions = function(props) {
return {
title: '',
headerLeft: <Button onPress={() => props.navigation.navigate('DrawerNavigator')} title= "=" />
}
};
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor:'teal' }}>
<Text style={styles.text}>FirstScreen</Text>
<Button
title="Next"
onPress={() => this.props.navigation.navigate('Second')}
/>
</View>
);
}
}
class SecondScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent:
'center', backgroundColor:'pink' }}>
<Text>Second Screen</Text>
<Button
title="Next"
onPress={() => this.props.navigation.navigate('Third')}
/>
</View>
);
}
}
class ThirdScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center", backgroundColor:'coral' }}>
<Text>Third Screen</Text>
<Button
title="Next"
onPress={() => this.props.navigation.navigate('First')}
/>
</View>
);
}
}
class FourthScreen extends React.Component {
static navigationOptions = function(props) {
return {
title: '',
headerLeft: <Button onPress={() => props.navigation.navigate('DrawerNavigator')} title= "=" />
}
};
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor:'orange' }}>
<Text style={styles.text}>4th Screen</Text>
<Button
title="Next"
onPress={() => this.props.navigation.navigate('Fifth')}
/>
</View>
);
}
}
class FifthScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent:
'center', backgroundColor:'purple' }}>
<Text>5th Screen</Text>
<Button
title="Next"
onPress={() => this.props.navigation.navigate('Sixth')}
/>
</View>
);
}
}
class SixthScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center", backgroundColor:'lightblue' }}>
<Text>6th Screen</Text>
<Button
title="Next"
onPress={() => this.props.navigation.navigate('Fourth')}
/>
</View>
);
}
}
/ ---------------堆栈导航器------------- /
const StackNavigator1 = createStackNavigator({
First: {screen: FirstScreen},
Second: {screen: SecondScreen},
Third: {screen: ThirdScreen}
});
const StackNavigator2 = createStackNavigator({
Fourth: {screen: FourthScreen},
Fifth: {screen: FifthScreen},
Sixth: {screen: SixthScreen}
});
/ --------------- 抽屉导航器------------- /
const DrawerNavigator = createDrawerNavigator(
{
StackNavigator1: {
screen: StackNavigator1
},
StackNavigator2: {
screen: StackNavigator2
}
}
);
export default createAppContainer(DrawerNavigator);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});