【问题标题】:How to add a customHeaderLeft button on a screen with goBack functionality using react-navigation in React Native?如何在 React Native 中使用 react-navigation 在具有 goBack 功能的屏幕上添加 customHeaderLeft 按钮?
【发布时间】:2021-08-27 07:21:26
【问题描述】:
<NavigationContainer>
  <Stack.Navigator screenOptions={{}}>
    <Stack.Screen name="home" component={Home} />
    <Stack.Screen
      name="MyProfile"
      component={Profile}
      options={{
        headerTintColor: 'white',
        title: 'My Profile',
        headerTransparent: true,
        headerShadowVisible: false,
        headerRight: () => (
          <TouchableOpacity>
            <DrawerIcon size={30} color={'white'} name="md-reorder-two-sharp" />
          </TouchableOpacity>
        ),
        headerLeft: () => (
          <TouchableOpacity>
            <Ionicons
              name="arrow-back-sharp"
              size={22}
              color="white"
              style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }}
            />
          </TouchableOpacity>
        ),
      }}
    />
  </Stack.Navigator>
  <ModalPortal />
</NavigationContainer>

下面是我用于堆栈导航的代码,我想从那个屏幕返回

1.headerLeft: () => <TouchableOpacity>
  <Ionicons name="arrow-back-sharp" size={22} color="white" style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }} />
  </TouchableOpacity>
}} />

【问题讨论】:

    标签: javascript react-native react-native-android react-native-ios react-native-navigation


    【解决方案1】:

    有两种方法可以实现这一目标

    1.) 在屏幕上设置options

    你可以使用useLayoutEffect钩子来实现这个

    在您要放置此标题的屏幕上,即Profile 屏幕上,只需添加以下代码

    React.useLayoutEffect(() => {
      navigation.setOptions({
        headerLeft: () => (
          <Ionicons
            name="arrow-back-sharp"
            size={22}
            color="white"
            style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }}
            onPress={() => navigation.goBack()}
            // make sure you destructure the navigation variable from the props
            // or otherwise you'll have to write it like this
            // onPress={() => props.navigation.goBack()}
          />
        ),
      });
    }, [navigation]);
    

    你的导航容器应该看起来像

    <NavigationContainer>
      <Stack.Navigator screenOptions={{}}>
        <Stack.Screen name="home" component={Home} />
        <Stack.Screen
          name="MyProfile"
          component={Profile}
          options={{
            headerTintColor: 'white',
            title: 'My Profile',
            headerTransparent: true,
            headerShadowVisible: false,
          }}
        />
      </Stack.Navigator>
      <ModalPortal />
    </NavigationContainer>;
    

    看看Working Example这里

    2.) 在 Navigation Container 中设置 headerLeftheaderRight 属性

    在 NavigationContainer 中设置属性,像这样

    <NavigationContainer>
      <Stack.Navigator screenOptions={{}}>
        <Stack.Screen name="home" component={Home} />
        <Stack.Screen
          name="MyProfile"
          component={Profile}
          options={({ navigation, route }) => ({
            headerTintColor: 'white',
            title: 'My Profile',
            headerTransparent: true,
            headerShadowVisible: false,
            headerRight: () => (
              <TouchableOpacity>
                <DrawerIcon size={30} color={'white'} name="md-reorder-two-sharp" />
              </TouchableOpacity>
            ),
            headerLeft: () => (
              <TouchableOpacity onPress={() => navigation.goBack()}>
                <Ionicons
                  name="arrow-back-sharp"
                  size={22}
                  color="white"
                  style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }}
                />
              </TouchableOpacity>
            ),
          })}
        />
      </Stack.Navigator>
      <ModalPortal />
    </NavigationContainer>
    

    看看Working Example这里

    【讨论】:

    • 常量导航 = useNavigation(); onPress={() => navigation.goBack()} 显示错误找不到导航对象。您的组件在 NavigationContainer 中吗?
    • navigation 5 可以,但是navigation 6 怎么做呢?
    • 感谢兄弟第一种方法是正确的,因为如果我使用第二种方法,那么其他属性就不起作用
    • @RutvikPrajapati 是的,这两种方式都可以使用React Navigation 6。只需确保您的语法正确或查看我在两种方式的答案中添加的 Snack 即可
    【解决方案2】:

    您提供了一个TouchableOpacity 作为后退按钮,但您没有指定任何onPress 回调。您应该为您的TouchableOpacity 提供一个onPress 回调。

    <NavigationContainer>
      <Stack.Navigator screenOptions={{}}>
        <Stack.Screen name='home' component={Home} />
        <Stack.Screen 
          name='MyProfile'
          component={Profile} 
          options={({ navigation, route }) => ({
            headerTintColor: 'white',
            title: 'My Profile',
            headerTransparent: true,
            headerShadowVisible: false,
            headerRight: () => (
              <TouchableOpacity>
                <DrawerIcon size={30} color={'white'} name='md-reorder-two-sharp' />
              </TouchableOpacity>
            ),
            headerLeft: () => (
              <TouchableOpacity onPress={()=> navigation.goBack()}>
                <Ionicons name="arrow-back-sharp" size={22} color="white" style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }} />
              </TouchableOpacity>
            )
          })} 
        />
      </Stack.Navigator>
      <ModalPortal />
    </NavigationContainer>
    

    如果你只是想改变后退按钮的外观,你最好使用react-navigation堆栈的headerBackImage props。

    这是Live Snack Example

    【讨论】:

    • 这个东西会起作用,但我也想应用 headerTransparent 的其他道具:是的,如果我使用这种技术,这些道具不适用,我该怎么办?
    • 其他属性不起作用是什么意思?
    • 我使用了 headerTransparent: true 它将显示我用作背景图像的背景图像,而不是默认的白色,而 headertint 颜色默认为黑色,我使用白色作为色调。因此,如果我使用这种方法,这些属性将不起作用,因此我在配置文件屏幕上的 useLayoutEffect 中使用导航选项,并在那里设置实现标题,如上所述,@kartikey 回答的代码您可以通过克隆我的 repo github.com/rutvikER1999/RN_Project2.git 来签入
    • 你的答案是对的,但我希望我的标题透明,这就是原因
    • @RutvikPrajapati 这也应该与其他道具一起使用。如果您看一下零食并导航到个人资料屏幕。您可以看到配置文件屏幕具有透明标题。
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2019-10-16
    相关资源
    最近更新 更多