首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >静态设置堆栈导航器的标题

静态设置堆栈导航器的标题
EN

Stack Overflow用户
提问于 2020-03-15 22:13:05
回答 1查看 28关注 0票数 0

假设我有2个导航器,如下:

代码语言:javascript
复制
const StackNavigator = () => {
  const theme = useTheme();

  return (
    <Stack.Navigator
      initialRouteName="BottomTabs"
      headerMode="screen"
      screenOptions={{
        header: Header,
      }}>
      <Stack.Screen
        name="BottomTabs"
        component={BottomTabs}
        options={({route, navigation}) => {
          console.log('get bottom tab title', route, navigation)
          const routeName = route.state
            ? route.state.routes[route.state.index].name
            : 'NOTITLE';
          return {headerTitle: routeName};
        }}
      />
    </Stack.Navigator>
  );
};

Stack导航器加载另一个导航器BottomTabs

代码语言:javascript
复制
const BottomTabs = props => {
  const theme = useTheme();
  const tabBarColor = theme.dark
    ? overlay(6, theme.colors.surface)
    : theme.colors.surface;

  return (
    <Tab.Navigator
        initialRouteName="TaskList"
        shifting={true}
        activeColor={theme.colors.primary}
        inactiveColor={color(theme.colors.text)
          .alpha(0.6)
          .rgb()
          .string()}
        backBehavior={'initialRoute'}
        sceneAnimationEnabled={true}>
        <Tab.Screen
          name="TaskList"
          component={TaskListScreen}
          options={{
            tabBarIcon: ({focused, color}) => (
              <FeatherIcons color={color} name={'check-square'} size={23} />
            ),
            tabBarLabel: 'InboxLabel',
            tabBarColor,
            title: 'Inbo title',
          }}
        />
        <Tab.Screen
          name="Settings"
          component={SettingsScreen}
          options={{
            tabBarIcon: ({focused, color}) => (
              <FeatherIcons color={color} name={'settings'} size={23} />
            ),
            tabBarLabel: 'SeetingsLabel',
            tabBarColor,
            title: 'Settings title',
          }}
        />
      </Tab.Navigator>
  );
};

我想根据从BottomTabs加载的屏幕来更改Stack标题。尝试将title选项传递到BottomTabs中的单个屏幕不起作用。

如何根据从子进程加载的屏幕更改Stack导航器的标题?

EN

回答 1

Stack Overflow用户

发布于 2020-03-15 23:11:33

您可以像这样自定义headerTitle:

代码语言:javascript
复制
<Stack.Screen
        name="BottomTabs"
        component={BottomTabs}
        options={({route}) => {
          let title;
          const routeName = route.state
            ? route.state.routes[route.state.index].name
            : route.params && route.params.screen
            ? route.params.screen
            : 'TaskList';
          switch (routeName) {
            case 'TaskList':
              title = 'Tasks screen';
              break;
            case 'Settings':
              title = 'Settings screen';
              break;
            default:
              return routeName;
          }
          return {headerTitle: title};
        }}
      />

重要提示:在任何导航之前,route.state都是未定义的。在此之后,堆栈创建它的导航状态,您的屏幕name道具是可用的。

Reference

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60693806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档