Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-navigation space between left button and title on android

I'm using react-navigation for navigate between screen.
In ios it display on center and working properly but in android it display left side and space between back button and title.

enter image description here

I want to remove space between back button and title on android.

My Code

class Detail extends Component {
    .
    .
    .
    static navigationOptions = ({ navigation }) => {
        const {state} = navigation;
        return {
            headerTitle: "title",
            headerStyle: {
                borderBottomColor: 'transparent',
                borderBottomWidth: 0,
                backgroundColor: 'transparent',

                elevation: 0 ,
                shadowOpacity: 0,
                shadowColor: 'transparent',
                shadowRadius: 0,
                shadowOffset: {
                    width: 0,
                    height: 0
                }
            },
            headerTitleStyle: {
              color: 'white',
              width: width-72,
            },
            headerBackTitleStyle: {
              color: 'white',
            },
            headerTintColor: 'white',
            headerBackground: (
                <LinearGradient
                    colors={[MyConstants.colorNavbarStart, MyConstants.colorNavbarEnd]}
                    style={{ flex: 1 ,padding: 0}}
                    start={{x: 0, y: 0.5}}
                    end={{x: 1, y: 0.5}} />
            ),
            headerRight: (
            <TouchableOpacity>
              <View style={{padding: 8}}>
                <Image source={MyConstants.imgShareArrow} style={{height:20, width: 20}} />
              </View>
            </TouchableOpacity>
            ),
            headerLeft: (
            <TouchableOpacity onPress={ () => {navigation.pop()}}>
              <View style={{padding: 8}}>
                <Image source={MyConstants.imgBackArrow} style={MyStyle.backButton} />
              </View>
            </TouchableOpacity>
            )
        };
    };
}

In above code width is screen width and i'm using -72 because left and right button width.
I'm also using marginLeft in minus but it cut the title.

like image 211
Bhaumik Surani Avatar asked Aug 09 '18 05:08

Bhaumik Surani


People also ask

Does react native navigation still have the title centered problem?

As of today, with the latest version of React Native Navigation, this issue still persists. In this thread there a number of "hacks" presented but the real issue is obvious in the library itself. Just to reiterate the problem; on Android, the title is not centered correctly when a button on the left is shown (like a Back button).

How to go back to an existing screen in react?

You can go back to an existing screen in the stack with navigation.navigate ('RouteName'), and you can go back to the first screen in the stack with navigation.popToTop (). The navigation prop is available to all screen components (components defined as screens in route configuration and rendered by React Navigation as a route).

How do I add a back button to the navigation stack?

The header provided by the native stack navigator will automatically include a back button when it is possible to go back from the active screen (if there is only one screen in the navigation stack, there is nothing that you can go back to, and so there is no back button).

How do I programmatically trigger a back button in react?

Sometimes you'll want to be able to programmatically trigger this behavior, and for that you can use navigation.goBack ();. On Android, React Navigation hooks in to the hardware back button and fires the goBack () function for you when the user presses it, so it behaves as the user would expect.


Video Answer


1 Answers

to remove space between back button and title you can use:

headerTitleContainerStyle: {
  left: 0,
},
like image 85
John Avatar answered Oct 27 '22 11:10

John