11

I'm using native-base to create a React Native app:

I'm using the Header Component to display Body, Left and Right elements. According to the docs, the title should automatically center but it does not (as seen below).

Am I missing something simple here? Any help would be appreciated!

import { 
    Container,
    Header,
    Content,
    Left,
    Right,
    Body,
    Title,
    Icon
} from "native-base"

export default class Seminars extends React.Component{

    render(){
        return(
            <Container style={styles.container}>
                <Header style={styles.header}>
                    <Left>
                        <Icon name='arrow-back' />
                    </Left>
                    <Body>
                        <Title>Seminars</Title>
                    </Body>
                    <Right>
                        <Icon name='menu' />
                    </Right>
                </Header>
                <Content contentContainerStyle={styles.content} >
                    <Text>Content Here</Text>
                </Content>
            </Container>
        )
    }
}
const styles = StyleSheet.create({
    container: {

    },
    header: {
        paddingRight: 15,
        paddingLeft: 15
    },
    content: {
        display: "flex",
        flex: 1,
        justifyContent: "center",
        padding: 15
    }
});

enter image description here

Chris_S
  • 145
  • 1
  • 1
  • 7

7 Answers7

16

If you want the title to be in the center, you can add flex:1 in Left, Body and Right like this :

    return(
        <Container style={styles.container}>
            <Header style={styles.header}>
                <Left style={{flex:1}}>
                    <Icon name='arrow-back' />
                </Left>
                <Body style={{flex:1}}>
                    <Title>Seminars</Title>
                </Body>
                <Right style={{flex:1}}>
                    <Icon name='menu' />
                </Right>
            </Header>
            <Content contentContainerStyle={styles.content} >
                <Text>Content Here</Text>
            </Content>
        </Container>
    )

And this is the result :

enter image description here

I hope this answer can help you :)

6

This answer can help you, worked for me.

        <Header style={{backgroundColor:'#ff2929'}}>
        <Left style={{flex: 1}}>
            <Button transparent style={{width: 65}}>
                <Icon style={{color:"#fff"}} type="MaterialIcons" name={this.props.imageLeft}/>
            </Button>
        </Left>
        <Body style={{flex: 3,justifyContent: 'center'}}>
        <Title style={{color: '#fff',alignSelf:'center'}}>{this.props.headerTitle}</Title>
        </Body>
        <Right style={{flex: 1}}>
            <Button transparent style={{width: 65}}>                    
                <Icon style={{color:this.props.color}} type="MaterialIcons" name={this.props.imageRight}/>
            </Button>
        </Right>
        </Header>
Ivy A.
  • 101
  • 1
  • 5
2

You can also try this one:

      <Header>
          <Left style={{ flex: 1 }}>
            <Icon name="arrow-back" />
          </Left>
          <Body style={{ flex: 1 }}>
            <Title style={{ alignSelf: "center" }}>Seminars</Title>
          </Body>
          <Right style={{ flex: 1 }}>
            <Icon name="menu" />
          </Right>
        </Header>
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
1

I find the best way to do this and its Work for me.

<Header transparent>
    <Left style={{ flex: 1 }}>
        <Icon name='arrow-back' />
    </Left>
    <Body style={{ flex: 1 }}>
        <Title style={{ justifyContent: 'center', color: '#9fabdd' }}>Home</Title>
    </Body>
    <Right style={{ flex: 1 }}>
        <Icon name='arrow-back' />
    </Right>
</Header>
kautikmistry
  • 686
  • 6
  • 10
1
<Header>
  <Left style={{flex: 1}} />
  <Body style={{flex: 1}}>
    <Title style={{alignSelf: 'center'}}>Header</Title>
  </Body>
  <Right style={{flex: 1}} />
</Header>
Ankit Adlakha
  • 1,436
  • 12
  • 15
0
static navigationOptions = ({ navigation }) => {
    return {

        headerTitle: (

  <Image  style={{width: 50, height: 10}} alignItems='center' source={require('../assets/zazzy.png')} />
  </View>
 ),
        headerLeft: (
            <View style={{ padding: 10 }}>
                <Ionicons name="ios-apps" color='gold' size={24} onPress={() => navigation.navigate('DrawerOpen')} />
            </View>
        ),
         headerRight: (
            <View style={{ padding: 10 }}>
                <Ionicons name="md-search" color='silver'  size={24} onPress={() => navigation.navigate('DrawerOpen')} />
            </View>
        )

    }
}
render() {
    return (
        <HomeScreenTabNavigator screenProps={{ navigation: this.props.navigation }} />

    )
}

}

  • 1
    Welcome to SO. Please format your answers properly. And please provide some explanation rather than code only. Thanks! – petezurich Jul 12 '18 at 11:07
0
      <Container style={styles.container}>
            <Header style={styles.header}>
                <Left style={{flex:1}}>
                    <Icon name='arrow-back' />
                </Left>
                <Body style={{flex:1}>
                    <Title style={{width:'100%'}}>Seminars</Title>
                </Body>
                <Right style={{flex:1}}>
                    <Icon name='menu' />
                </Right>
            </Header>
            <Content contentContainerStyle={styles.content} >
                <Text>Content Here</Text>
            </Content>
        </Container>

I found this as a solution when the left and right items are of unequal sizes, works for both android and iOS

  • 2
    Outside of the title styling, this appears to be identical to the accepted answer from three years ago. Am I missing something? Does setting the width on the title provide a key difference in addressing the question? If so, I’d recommend editing your answer to make that really clear to readers. – Jeremy Caney May 20 '20 at 17:41
  • this styling needs to be done if we want it get centered, the width: 100% is the key takeaway – pewdie2pie Jun 20 '20 at 13:35