How can we center the react-navigation title title?
Use headerTitleStyle :
static navigationOptions = {
headerTitleStyle: { alignSelf: 'center' },
title: 'Center Title',
}
Changed on 2019/03/12:
In 2018, after the release of react-navigation v2 (April 7, 2018), for some reason alignSelf
no longer worked. Here's a new working way using headerLayoutPreset . from @HasanSH:
const HomeActivity_StackNavigator = createStackNavigator({
Home: {screen: Main},
}, {headerLayoutPreset: 'center'});
source to share
The accepted answer only works for me if there is no Back button on the left. In this case, you need to set the empty view to the right in order to center it correctly.
static navigationOptions = {
headerTitleStyle: { alignSelf: 'center' },
title: 'Center Title',
headerRight: (<View />)
}
source to share
The best way to do this is to implement what the documentation says: internally, StackNavigatorConfig
assign an optional property like this:
createStackNavigator({
{ ... // your screens},
{ ...,
headerLayoutPreset: 'center' // default is 'left'
})
headerLayoutPreset
- Determines how to arrange the header components.
By doing this, you don't have to spoil the style at all. And this will apply to all nested screens in that stack.
Check the source
source to share
You should add headerLayoutPreset: 'center' to your createeStackNavigator function.
This is the only sure way:
const someStack = createStackNavigator({
ConfigurationScreen: ConfigurationScreen,
PreferencesScreen: PreferencesScreen},
{ headerLayoutPreset: 'center' });
Link: https://github.com/react-navigation/react-navigation/pull/4588
source to share
Make sure you check for problems before a stack overflow occurs, usually more useful. question about your problem But as himanshu said in the comments, you need to access the title's style property to customize the title the way you want.
static navigationOptions = {
header: (navigation) => ({
title: <Text style={{
color: 'rgba(0, 0, 0, .9)',
fontWeight: Platform.OS === 'ios' ? '600' : '500',
fontSize: Platform.OS === 'ios' ? 17 : 18,
alignSelf: 'center'
}}>Filters</Text>,
right: <SearchButton />,
left: <CancelButton />,
})
};
As shown in this release, I am assuming that you have already managed to find a solution, as you did recently. But for those facing this problem, it might be helpful.
source to share
You can set the center of the title title for android in the stack navigator to change the navigation with this file:
node_modules\react-navigation\src\views\Header.js
Change this code in your Header.js file: -
title: {
bottom: 0,
left: TITLE_OFFSET,
right: TITLE_OFFSET,
top: 0,
position: 'absolute',
alignItems: Platform.OS === 'ios' ? 'center' : 'center',
},
source to share