React-native更改堆栈导航器标题基于从嵌套抽屉导航中选择的内容

问题描述 投票:0回答:1

我有一个嵌套在堆栈导航器中的抽屉导航器,就像这样

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'
    }),
  },
})

这给了我想要的布局,在IOS和android上都显示了“标题栏”,并使抽屉导航成为导航应用程序的主要方法。但是,我希望堆栈导航器的标题道具显示从抽屉导航器中选择的任何屏幕的名称,而不是静态字符串。这种当前设置是否可行?也许这是不正确的方法 - 我是反应原生导航的新手。

reactjs react-native react-navigation
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.