简体   繁体   中英

How do I change the back button image in stack navigator in react native?

I've already seen documents but I don't really understand and I want somebody to show me how to change the image of the back button in react native. enter image description here

I want to change the back arrow to a chevron or something. Could you please show me the code on how to do this?

In your StackNavigator, provide the screenOptions prop with headerBackImageSource set to the back arrow icon or image you want.

    <Stack.Navigator
      initialRouteName="Home"
      screenOptions={{
        headerBackImageSource: `...image source...`
      }}
    >

The way I handle the default header of stack navigator is by creating a custom one. Every component in the main stack exports a back method so you can use it to create a event listener for your custom arrow like in the example below.

MAIN COMPONENT:

function Home({ navigation }) {

  return(
    <>
       <CustomHeader
         title="Home Page"
         handleBack={() => {
           navigation.goBack()
         }}
       />  
    </>
  )
}

export default Home

CUSTOM HEADER COMPONENT:

function CustomHeader(props) {
  const title = props.title

  const handleBack = () => {
    props.handleBack()
  }

  return(
    <View>
      <Pressable
        onPress={handleBack}
      >
        <Image
          source={backArrowIcon}
          resizeMode="contain"
        />
      </Pressable>
      <Text>
        {title}
      </Text>
    </View>
  )
}

export default CustomHeader

You just need to style that and import all the components like View, Text, CustomHeader etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM