我有一个嵌套在堆栈导航器中的抽屉导航器,如此
const DrawerNavigation = DrawerNavigator({
Home: {
screen: Home,
navigationOptions: {
drawerLabel: 'Home',
drawerIcon: () => (
<MaterialCommunityIcons name={'home'} size={25} />
)
},
},
Customers: {
screen: Customers,
navigationOptions: {
drawerLabel: 'Customers',
drawerIcon: () => (
<MaterialCommunityIcons name={'account-switch'} size={25} />
)
},
},
}, {
drawerPosition: 'right',
})
const Navigation = StackNavigator({
Home: {
screen : DrawerNavigation,
navigationOptions: ({ navigation }) => ({
title: navigation.state.routeName, // Always 'Home'
}),
},
})
这给了我想要的布局,标题栏&#34;它显示在IOS和android上,并使抽屉导航成为导航应用程序的主要方法。但是,我希望堆栈导航器的标题道具显示从抽屉导航器中选择的任何屏幕的名称,而不是静态字符串。这种当前设置是否可行?也许这是不正确的方法 - 我是反应原生导航的新手。
答案 0 :(得分:1)
您可以在每个屏幕的navigationOptions中为每个屏幕添加标题。
const DrawerNavigation = DrawerNavigator({
Home: {
screen: Home,
navigationOptions: {
drawerLabel: 'Home',
title: 'Home, sweet home!' // <=== add this
drawerIcon: () => (
<MaterialCommunityIcons name={'home'} size={25} />
)
},
},
Customers: {
screen: Customers,
navigationOptions: {
drawerLabel: 'Customers',
title: 'Dear customers', // <=== and this
drawerIcon: () => (
<MaterialCommunityIcons name={'account-switch'} size={25} />
)
},
},
}, {
drawerPosition: 'right',
})
const Navigation = StackNavigator({
Home: {
screen : DrawerNavigation,
navigationOptions: ({ navigation }) => ({
title: navigation.state.routeName, // <=== remove this
}),
},
})
此外,您可以在每个屏幕组件上设置标题,更多详细信息here