【问题标题】:How to put the title of screens in header of each screen?如何将屏幕标题放在每个屏幕的标题中?
【发布时间】:2021-01-08 08:50:49
【问题描述】:

我需要帮助我了解屏幕在我的应用程序中的行为。

我刚刚注意到,无论我正在浏览什么屏幕,标题顶部的屏幕标题始终是“订单”。 当然,我希望每个标题都显示屏幕标题,所以我认为我的导航文件中缺少一些信息。

但是,我真的不知道在哪里或如何在标题中添加屏幕标题。 但是,我将屏幕的名称/标题放在每个堆栈中。屏幕。

请您指导我,帮助我并向我解释我的错误在哪里,好吗? 感谢您阅读我。

const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();


const screenOptionStyle = {
  headerStyle: {
    backgroundColor: "#F78400",
  },
  headerTintColor: "white",
  headerBackTitle: "Back",
  backgroundColor:'#f7f6f6'
};


export const MainStackNavigator = () => {
  return (
   <Stack.Navigator screenOptions={screenOptionStyle}>      
      <Stack.Screen name = 'Orders' component = {BottomTabNavigator} options={{ title: i18n.t("orders.title") }}/> 
      <Stack.Screen name = 'Authentication' component = {Authentication} options={{title: i18n.t("authentication.title"), headerShown: false }}/>
      <Stack.Screen name = 'Account' component = {Account} options={{ title: i18n.t("account.title") }}/>
      <Stack.Screen name = 'Activities' component = {Activities} options={{ title: i18n.t("activities.title") }}/> 
      <Stack.Screen name = 'Contact' component = {Contact} options={{ title: i18n.t("contact.title") }}/> 
      <Stack.Screen name = 'Login' component = {Login} options={{ title: i18n.t("login.title"), headerShown: false }}/>
      <Stack.Screen name = 'Register' component = {Register} options={{ title: i18n.t("register.title"), headerShown: false }}/>
      <Stack.Screen name = 'Reset' component = {Reset} options={{ title: i18n.t("reset.title") }}/>
      <Stack.Screen name = 'Tools' component = {Tools} options={{ title: i18n.t("tools.title") }}/>
      <Stack.Screen name = 'Scan' component = {Scan} options={{ title: i18n.t("scan.title") }}/>      
      <Stack.Screen name = 'Current' component = {Current} options={{ title: i18n.t("current.title") }}/>
      <Stack.Screen name = 'Completed' component = {Completed} options={{ title: i18n.t("completed.title") }}/>
      <Stack.Screen name = 'Products' component = {Products} options={{ title: i18n.t("products.title") }}/>
      <Stack.Screen name = 'ProductDetails' component = {ProductDetails} options={{ title: i18n.t("fiche.title") }}/>
      <Stack.Screen name = 'Information' component = {Information} options={{ title: i18n.t("information.title") }}/>
      <Stack.Screen name = 'Photos' component = {Photos} options={{ title: i18n.t("photos.title") }}/>
      <Stack.Screen name = 'Stock' component = {Stock} options={{ title: i18n.t("stock.title") }}/>
      <Stack.Screen name = 'Terms' component = {Terms} options={{ title: i18n.t("terms.title") }}/> 
      <Stack.Screen name = 'About' component = {About} options={{ title: i18n.t("about.title") }}/>      
      <Stack.Screen name = 'Tickets' component = {Tickets} options={{ title: i18n.t("tickets.title") }}/>
      <Stack.Screen name = 'Dashboard' component = {Dashboard} options={{ title: i18n.t("dashboard.title") }}/>
      <Stack.Screen name = 'Settings' component = {Settings} options={{ title: i18n.t("settings.title") }}/>
      <Stack.Screen name = 'Welcome' component = {Welcome} options={{ title: i18n.t("welcome.title") }}/>
      <Stack.Screen name = 'BottomTabNavigator' component = {BottomTabNavigator} options={{ title: i18n.t("welcome.title") }}/>
    </Stack.Navigator>
  );
}

export const BottomTabNavigator = () => {
  return (
    <Tab.Navigator tabBarOptions={{
      activeTintColor: 'black',
      labelStyle: {fontSize: 12, color: 'white'}, 
      style: {backgroundColor: '#F78400'},
    }}>      
      <Tab.Screen
          name={i18n.t("orders.title")}
          component={Orders}
          options={{
              tabBarIcon: ({ focused, horizontal, tintColor }) => {
                return (
                  <Image
                    source={require("../assets/images/orders.png")}
                    style={styles.icon}
                  />
                );
              }
          }}
        />
        <Tab.Screen
          name={i18n.t("dashboard.title")}
          component={Dashboard}
          options={{
            tabBarIcon: ({ focused, horizontal, tintColor }) => {
              return (
                <Image
                  source={require("../assets/images/dashboard.png")}
                  style={styles.icon}
                />
              );
            }
          }}
        />
        <Tab.Screen
          name={i18n.t("tools.title")}
          component={Tools}
          options={{
            tabBarIcon: ({ focused, horizontal, tintColor }) => {
              return (
                <Image
                  source={require("../assets/images/tools.png")}
                  style={styles.icon}
                />
              );
            }
          }}
        />
        <Tab.Screen
          name={i18n.t("settings.title")}
          component={Settings}
          options={{
            tabBarIcon: ({ focused, horizontal, tintColor }) => {
              return (
                <Image
                  source={require("../assets/images/settings.png")}
                  style={styles.icon}
                />
              );
            }
          }}
        />
    </Tab.Navigator>
  );
};

【问题讨论】:

    标签: react-native navigation header title


    【解决方案1】:

    由于您在堆栈屏幕中嵌套了 BottomTabNavigation。堆栈屏幕的标题将始终相同。

    import {getFocusedRouteNameFromRoute} from '@react-navigation/native';
    const Stack = createStackNavigator();
    const Tab = createBottomTabNavigator();
    
    const screenOptionStyle = {
      headerStyle: {
        backgroundColor: '#F78400',
      },
      headerTintColor: 'white',
      headerBackTitle: 'Back',
      backgroundColor: '#f7f6f6',
    };
    
    function getHeaderTitle(route) {
      const routeName = getFocusedRouteNameFromRoute(route) ?? 'OrdersTab';
    
      switch (routeName) {
        case 'OrdersTab':
          return i18n.t('orders.title');
        case 'DashboardTab':
          return i18n.t('dashboard.title');
        case 'ToolsTab':
          return i18n.t('tools.title');
        case 'SettingsTab':
          return i18n.t('settings.title');
      }
    }
    
    export const MainStackNavigator = () => {
      return (
        <Stack.Navigator screenOptions={screenOptionStyle}>
          <Stack.Screen
            name="Orders"
            component={BottomTabNavigator}
            options={({route}) => ({
              headerTitle: getHeaderTitle(route),
            })}
          />
          <Stack.Screen
            name="Authentication"
            component={Authentication}
            options={{title: i18n.t('authentication.title'), headerShown: false}}
          />
          <Stack.Screen
            name="Account"
            component={Account}
            options={{title: i18n.t('account.title')}}
          />
          <Stack.Screen
            name="Activities"
            component={Activities}
            options={{title: i18n.t('activities.title')}}
          />
          <Stack.Screen
            name="Contact"
            component={Contact}
            options={{title: i18n.t('contact.title')}}
          />
          <Stack.Screen
            name="Login"
            component={Login}
            options={{title: i18n.t('login.title'), headerShown: false}}
          />
          <Stack.Screen
            name="Register"
            component={Register}
            options={{title: i18n.t('register.title'), headerShown: false}}
          />
          <Stack.Screen
            name="Reset"
            component={Reset}
            options={{title: i18n.t('reset.title')}}
          />
          <Stack.Screen
            name="Tools"
            component={Tools}
            options={{title: i18n.t('tools.title')}}
          />
          <Stack.Screen
            name="Scan"
            component={Scan}
            options={{title: i18n.t('scan.title')}}
          />
          <Stack.Screen
            name="Current"
            component={Current}
            options={{title: i18n.t('current.title')}}
          />
          <Stack.Screen
            name="Completed"
            component={Completed}
            options={{title: i18n.t('completed.title')}}
          />
          <Stack.Screen
            name="Products"
            component={Products}
            options={{title: i18n.t('products.title')}}
          />
          <Stack.Screen
            name="ProductDetails"
            component={ProductDetails}
            options={{title: i18n.t('fiche.title')}}
          />
          <Stack.Screen
            name="Information"
            component={Information}
            options={{title: i18n.t('information.title')}}
          />
          <Stack.Screen
            name="Photos"
            component={Photos}
            options={{title: i18n.t('photos.title')}}
          />
          <Stack.Screen
            name="Stock"
            component={Stock}
            options={{title: i18n.t('stock.title')}}
          />
          <Stack.Screen
            name="Terms"
            component={Terms}
            options={{title: i18n.t('terms.title')}}
          />
          <Stack.Screen
            name="About"
            component={About}
            options={{title: i18n.t('about.title')}}
          />
          <Stack.Screen
            name="Tickets"
            component={Tickets}
            options={{title: i18n.t('tickets.title')}}
          />
          <Stack.Screen
            name="Dashboard"
            component={Dashboard}
            options={{title: i18n.t('dashboard.title')}}
          />
          <Stack.Screen
            name="Settings"
            component={Settings}
            options={{title: i18n.t('settings.title')}}
          />
          <Stack.Screen
            name="Welcome"
            component={Welcome}
            options={{title: i18n.t('welcome.title')}}
          />
          <Stack.Screen
            name="BottomTabNavigator"
            component={BottomTabNavigator}
            options={{title: i18n.t('welcome.title')}}
          />
        </Stack.Navigator>
      );
    };
    
    export const BottomTabNavigator = () => {
      return (
        <Tab.Navigator
          tabBarOptions={{
            activeTintColor: 'black',
            labelStyle: {fontSize: 12, color: 'white'},
            style: {backgroundColor: '#F78400'},
          }}>
          <Tab.Screen
            name={'OrdersTab'}
            component={Orders}
            options={{
              tabBarIcon: ({focused, horizontal, tintColor}) => {
                return (
                  <Image
                    source={require('../assets/images/orders.png')}
                    style={styles.icon}
                  />
                );
              },
            }}
          />
          <Tab.Screen
            name={'DashboardTab'}
            component={Dashboard}
            options={{
              tabBarIcon: ({focused, horizontal, tintColor}) => {
                return (
                  <Image
                    source={require('../assets/images/dashboard.png')}
                    style={styles.icon}
                  />
                );
              },
            }}
          />
          <Tab.Screen
            name={'ToolsTab'}
            component={Tools}
            options={{
              tabBarIcon: ({focused, horizontal, tintColor}) => {
                return (
                  <Image
                    source={require('../assets/images/tools.png')}
                    style={styles.icon}
                  />
                );
              },
            }}
          />
          <Tab.Screen
            name={'SettingsTab'}
            component={Settings}
            options={{
              tabBarIcon: ({focused, horizontal, tintColor}) => {
                return (
                  <Image
                    source={require('../assets/images/settings.png')}
                    style={styles.icon}
                  />
                );
              },
            }}
          />
        </Tab.Navigator>
      );
    };
    

    【讨论】:

    • 我忘了问你,'??'是什么意思在你的功能中?非常感谢:)
    • 三元运算符,用英语说“否则”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2017-11-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2018-04-21
    • 2020-10-14
    相关资源
    最近更新 更多