In react native I have:
<View style={styles.navBar}> <Text>{'<'}</Text> <Text style={styles.navBarTitle}> Fitness & Nutrition Tracking </Text> <Image source={icon} style={styles.icon}/> </View>
with these styles:
{ navBar: { height: 60, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, navBarTitle: { textAlign: 'center', }, icon: { height: 60, resizeMode: 'contain', }, }
This is the effect I get:
This is the effect I want:
In the first example, the spacing between items is equal.
In the second example, each item is justified differently. The first item is left-justified. The second item is center-justified. The third, right-justified.
This question is similar, but it looks like react native does not support margin: 'auto'
. Furthermore, the other answers only work if you only care about left and right justification, but no one really addresses center justification without auto margin.
I am trying to make a navigation bar in react native. The vanilla ios version looks like this:
(source: apple.com)
How do I do something similar? I'm mainly concerned with centering.
Use textAlign: 'right' on the Text element (This approach doesn't change the fact that the Text fills the entire width of the View ; it just right-aligns the text within the Text .)
To align a component to the center or right with React Material UI, we can add a flex container with the Grid component. We add the Grid component and add the container prop to make it a flexbox container. Then we set the justify prop to flex-end to align the child components on the right side.
Use align-content utilities on flexbox containers to align flex items together on the cross axis. Choose from start (browser default), end , center , between , around , or stretch . To demonstrate these utilities, we've enforced flex-wrap: wrap and increased the number of flex items.
One way is to use nested View (flex containers) for 3 different regions and set flex:1 to left and right region
<View style={styles.navBar}> <View style={styles.leftContainer}> <Text style={[styles.text, {textAlign: 'left'}]}> {'<'} </Text> </View> <Text style={styles.text}> Fitness & Nutrition Tracking </Text> <View style={styles.rightContainer}> <View style={styles.rightIcon}/> </View> </View> const styles = StyleSheet.create({ navBar: { height: 60, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', backgroundColor: 'blue', }, leftContainer: { flex: 1, flexDirection: 'row', justifyContent: 'flex-start', backgroundColor: 'green' }, rightContainer: { flex: 1, flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center', backgroundColor: 'red', }, rightIcon: { height: 10, width: 10, resizeMode: 'contain', backgroundColor: 'white', } });
You could also set marginLeft: 'auto'
to the middle component. It should push it to the right. Also works for React Native
Source: https://hackernoon.com/flexbox-s-best-kept-secret-bd3d892826b6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With