I have a View inside a view. And I'm trying to vertically center the child view.
Below is the sample code:
<View style={globalStyle.text}>
<Text style={[globalStyle.name]}>
{name}
</Text>
<View style={globalStyle.timeContainer}>
<Image style={globalStyle.tinyIcon} source={require('./Thumbnails/time-tiny.png')}/>
<Text style={globalStyle.audio_count}>
{this.state.audioTime[rowID]}
</Text>
</View>
</View>
When there is text alone justifyContent: 'center' works fine to vertically center the child content. However when there is a child View involved it doesn't work. How can I achieve this?
Here is a picture of the same: I'm trying to make the text inside the white box vertically center aligned.
Related
I want my text and button at the top of the screen, but both the elements have a few gaps between, so I added marginBottom to my text and then that text went to the top. But I want that button to be at the top so I added marginBottom to the button too, but now it's not going to the top.
How can I position it to the top? Where is the style issue coming from? Please, explain my mistake as well.
import { StyleSheet, Text, View, Button } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<Text style={{ margin: 16, marginBottom: '80%', backgroundColor: 'black' }}>Hello World</Text>
<View style={{ marginBottom: '80%' }}>
<Button title='Click me' />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
You're using alignItems: 'center' and justifyContent: 'center'.
These two properties combined make the contents of container be positioned in the center. alignItems:center vertically centers the contents, and justifyContent:center horizontally centers the contents.
Since Text is the upper child element, giving it a marginBottom will space it out from the View element containing the Button. If you compare your screen with and without that marginBottom on the Text you'll notice that the button also moves slightly to the bottom if you include the marginBottom on the Text.
So in short, your code is doing exactly what you tell it to do: container tries to position all children to its center.
To fix this, you might want to remove the container:{ alignItems:'center', } property. This will most likely push all your elements to the top of the container. After that you might simply use marginTop on your Text element to push the contents down.
Change both marginBottom %80 to "auto".
return (
<View style={styles.container}>
<Text
style={{ margin: 16, marginBottom: "auto", backgroundColor: "white" }}
>
Hello World
</Text>
<View style={{ marginBottom: "auto" }}>
<Button title="Click me" />
</View>
</View>
);
I need to make a view top of text element. And the element inside View should be ordered bottom to top
three
two
one
text element
The text element should not be pushed down if number of top elements increases. How to achieve this?
I have tried like this. Got text element x,y, width, height by onlayout event
<>
<View>
<Text>Text</Text>
</View>
<View
style={{
top: textEl?.top - 20,
left: textE?.left,
position: 'absolute',
flexDirection:'column',
justifyContent:'flex-end'
}}>
<View>
<View>
<Text>one</Text>
</View>
<View>
<Text>two</Text>
</View>
<View>
<Text>three</Text>
</View>
</View>
</View>
</>;
CSS : flex-wrap: wrap-revers
React: flexWrap: 'wrap-revers'
I want to absolutely position a Font Awesome icon I have in a React Native project relative to the parent of a parent, not the icon's direct parent.
However, when I was looking into it, I found the following on the following page: https://reactnative.dev/docs/layout-props#position
position in React Native is similar to regular CSS, but everything is set to relative by default, so absolute positioning is always relative to the parent.
As such, if I have something like the following, how can I position the icon relative to the grandparent TouchableOpacity, and not the parent View? Thank you.
<TouchableOpacity
onPress={() => {}}
>
<View>
{/* Other markup here. */}
</View>
<View>
{/* Want to position this icon relative to TouchableOpacity, not View. */}
<FontAwesome name="heart" />
</View>
</TouchableOpacity>
If that's your markup, then you would want to have the View absolutely positioned like the following:
<TouchableOpacity
onPress={() => {}}
>
<View>
{/* Other markup here. */}
</View>
<View
style={{
position: 'absolute',
...
}}
>
<FontAwesome name="heart" />
</View>
</TouchableOpacity>
Now, the entire view is absolutely positioned in relation to the TouchableOpacity. Therefore the same applies to its children, aka. your icon.
I have tried all things to align vertically an icon and a text, Code:
<Danger color="secondary" style={{ flex:1,justifyContent: "center",alignItems: "center" }}>
<ErrorOutline
className={classes.warning}
/>
<text numberOfLines={1} style={{ textAlignVertical: "center" }}>
The last job was canceled
</text>
</Danger>
Things that I've tried : display, justifyContent, alignItems, flex, flexDirection, etc.
Any advice? Thanks!
Try out this inside render of react code
<div style={{display: 'flex', lineHeight: '40px'}}>
<img src="https://png.icons8.com/ios/50/000000/user-male-circle-filled.png" height="40"/>
<div>Welcome User!!!</div>
</div>
And for clear understanding you can find the entire React code for this particular example in this link: https://jsfiddle.net/r6yLmu8t/
You need to give lineHeight for the div tag in which the image/icon and text are there. And make sure that the icon/image height is equal to the lineHeight given to the div.
Hope this will achieve your need of text and icon to be on the same line and text to be at centre of icon/image
<Button light>
<View style={{flexGrow: 1, justifyContent:'center', alignItems: 'center'}}>
<Icon name='arrow-back' />
<Text>Back</Text>
</View>
</Button>
This is how I'm showing my image in React-Native:
<View>
<Text style={styles.myTitle}> This is the title 2 </Text>
<Image
style={{ width: null}}
source={require('./images/05.jpg')}
resizeMode="stretch"
/>
</View>
With the resizeMode="stretch" The Image is shown like this:
The same image when displayed with resizeMode="contain" :
<View>
<Text style={styles.myTitle}> This is the title 3</Text>
<Image
style={{ width: null}}
source={require('./images/05.jpg')}
resizeMode="contain"
/>
</View>
resizeMode="contain":
I want the image to be displayed like how it is in the second type, but there are unnecessary margins:
The margins in the first image are perfect but the full image is not shown. I always thought contain will take care of that but it's not helping here. All I want is the entire image to be shown without any extra margins.
Please help. Thanks.
You can use cover, but it will crop part of the image.
To make it work you will need to add a height property to your image:
<View>
<Text>This is the title 3</Text>
<Image
style={{ width: null, height: '100%' }}
source={require('./image.png')}
resizeMode="cover"
/>
</View>