Search code examples
javascriptreactjsreact-nativeapache-flexheader

Align two children differently in react native


I'd like to have a header with a back button to the left and a text/title in the center of the header.

Like this:

<View style={styles.topbar}>
    <TouchableOpacity>
        <Text style={styles.topbarButton}>{" <"}</Text>
    </TouchableOpacity>
    <View>
        <Text style={styles.topbarText}>Title</Text>
    </View>
</View>

I'm trying to use flex in styles.topbar but nothing of what I tried seems to work.

I am pretty new to flex and React Native so some help with the styles would be great.

Thanks all.


Solution

  • Here we go:

    <View style={{width:devicewidth,flexDirection:'row',height:40,top:0,position:'absolute'}}>
       <TouchableOpacity style={{left:0,position:'absolute',alignItems:'flex-    start'}}>
          <Text style={styles.topbarButton}>{" <"}</Text>
       </TouchableOpacity>
         <View style={{justifyContent:'center'}}>
          <Text style={styles.topbarText}>Title</Text>
         </View>
    </View>
    

    The above code will print back button at the top left and text at the center. Flex direction is used to indicate view to print in a row direction.