Stack components centered above each other in React Native - react-native

I'm trying to place two (or more) components above each others, centered vertically and horizontally in a view that wraps this content (i.e., the height of this outer view should be the max height of the components inside; idem for the width).
At first, absolute positioning looked like a good idea to have all the components centered above each other. However then I don't know how to get the outer view to wrap the content.
<View style={{ backgroundColor: 'black', alignItems: 'center', justifyContent: 'center'}}>
<View style={{position: 'absolute', width: 60, height: 60, backgroundColor: 'blue'}} />
<View style={{position: 'absolute', width: 70, height: 50, backgroundColor: 'red'}} />
<View style={{position: 'absolute', width: 50, height: 70, backgroundColor: 'green'}} />
</View>
With this examples, we cannot see any of the black background of the outer View (whose size is probable 0x0), whereas I'm expecting a 70x70 black square behind the three colored rectangles.

The outermost View will not automatically adjust to its children if their position is absolute.
You would have to make the outermost View's position absolute and calculate its size and position based on its children and the screen's width and height.
Another option is to create a background View as a child with a position of relative like this:
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<View style={{position: 'relative', width: 70, height: 70, backgroundColor: 'black'}} />
<View style={{position: 'absolute', width: 60, height: 60, backgroundColor: 'blue'}} />
<View style={{position: 'absolute', width: 70, height: 50, backgroundColor: 'red'}} />
<View style={{position: 'absolute', width: 50, height: 70, backgroundColor: 'green'}} />
</View>

Related

Problem with flex-box Layout in React Native

Let's say there is a square-shaped container,
How can make sure that B is centered while A is in the top-left corner like in the picture?
If I put B in a View and justifyContent to center, B is only centered to the space left between A and the bottom of the container. How do I stop A from pushing B down?
You can use position: 'absolute' for A in this case.
<View style={{ height: 100, width: 100, backgroundColor: 'red', justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ position: 'absolute', top: 0, left: 0, color: 'white' }} >A</Text>
<Text style={{ color: 'white' }} >B</Text>
</View>

How to position text into part of image background in react native?

I have image background with "3 level circle shape" (see image)
I would like to set text "hello" into center of circles. Looks like its almost imposible for all devices.
How can I do it?
Mu current code is:
return (
<ImageBackground source={background} resizeMode="cover" style={{ width: '100%', height: '100%' }}>
</ImageBackground>
I tried to use absolute position, but it is bronek on second device. Is there any trick?
Original image size (in asset folder) is 1080x2400
Thank you.
Why not create the circles manually? That way centering can be taken care of automatically. Example (you'll have to tweak values/add circles):
<View style={{ flex: 1, backgroundColor: '#F9F3EE' }}>
<View
style={{
backgroundColor: '#F6EFF2',
top: -100,
width: 700,
height: 700,
left: -(700 - Dimensions.get('window').width) / 2,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 350,
}}
>
<View
style={{
backgroundColor: '#ECE4DD',
width: 500,
height: 500,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 250,
}}
>
<View
style={{
backgroundColor: '#E2D7D0',
width: 300,
height: 300,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 150,
}}
>
<Text>Hi</Text>
</View>
</View>
</View>
</View>

Weird Margin behavior in React Native in combination with absolute position

I'm trying to position View elements in react native that have the position: "absolute" attribute in their parent container, but the positive and negative values seem not to be consistent. Let me show you by an example:
function Home(props) {
return (
<View style={{ justifyContent: "center", alignItems: "center", flex: 1 }}>
<View style={{ position: "absolute" }}>
<View style={{ backgroundColor: "blue", width: 100, height: 100 }}></View>
</View>
<View style={{ position: "absolute" }}>
<View style={{ backgroundColor: "yellow", width: 100, height: 100, marginTop: 200 }}></View>
</View>
<View style={{ position: "absolute" }}>
<View style={{ backgroundColor: "red", width: 100, height: 100, marginTop: -200 }}></View>
</View>
</View>
);
}
which produces an output that looks like this:
So I have three View elements packed in another View that has the position: "absolute" attribute. The first (blue) has no margin so it is exactly in the middle, the second (yellow) has a positive top margin of 200 and the third (red) a negative top margin of 200. It seems to be very odd why the red rectangle is further away than the yellow.
A marginTop: 200 and marginTop: -150 seem to have the same effect (in opposite directions). What is the problem here?
The reason for this is because you're using marginTop instead of top. The problem with margin and absolute positioning here is that you're actually changing the size of the parent of the colored boxes even if it doesn't look like it, instead of moving the colored boxes by a certain amount from the center.
So you're probably looking for something like this instead:
function Home(props) {
return (
<View style={{justifyContent: 'center', alignItems: 'center', flex: 1}}>
<View style={{position: 'absolute'}}>
<View style={{backgroundColor: 'blue', width: 100, height: 100}}></View>
</View>
<View style={{position: 'absolute'}}>
<View
style={{
backgroundColor: 'yellow',
width: 100,
height: 100,
top: 200,
}}></View>
</View>
<View style={{position: 'absolute'}}>
<View
style={{
backgroundColor: 'red',
width: 100,
height: 100,
top: -200,
}}></View>
</View>
</View>
);
}
To explain it differently: You're not really moving the colored boxes, but you're modifying the space on top of the colored boxes which pushes the boxes down.
If you add a borderWidth to the views around the different boxes you can see the effect that adding the margin to the box views has on the parent views.

Image borderRadius is not working in react native

I have used borderRadius in Image and loop it. It works in some images however others are rectangular. If I touch it then it shows the radius and disappears as soon as it is untouched. Using overflow hidden doesn't do the trick as well.
I'm confused I used the same style but the result is different. I've tested it on Android devices only.
https://snack.expo.io/#codebyte99/multiplearrays
code:
<TouchableOpacity activeOpacity={0.8}>
<ImageBackground
source={{
uri: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREX0q18KDbtN-obe1EFxAwNg27xgR_KItZ7U8MkXnH7zBCEr_ASQ",
}}
style={[
{
width: 200,
height: 80,
resizeMode: "center",
justifyContent: "flex-end",
alignItems: "center",
margin: 5,
marginRight: 0,
marginTop: 0,
marginBottom: 5,
borderRadius: 6,
overflow: "hidden",
},
]}
>
<Text>{childItem.title}</Text>
</ImageBackground>
</TouchableOpacity>;
You have to set the borderRadius to Image, not in style:
<Image source={{...}} borderRadius={6} .../>
And you don't set overflow: 'hidden' in image style, but in the View:
<View style={{ ..., overflow: 'hidden' }}>
<Image ..../>
</View>
Wrap the Image in a View with overflow: "hidden" and apply borderRadius to that, e.g.
<View
style={{
width: 30,
height: 30,
borderRadius: 15,
overflow: "hidden",
}}
>
<Image
style={{ width: 30, height: 30}}
source={{ uri }}
></Image>
</View>
(I could not find a way to get borderRadius to work on the image directly on Android.)
Applying the same fixed height and width to the image and borderRadius as half of the (height/width) will solve the border issue for android in react-native. Note: Make sure you have resizeMode as 'cover'
<Image key={index} source={{ 'uri' }} resizeMode='cover' style={{ height: 40, width: 40, borderRadius: 20 }} />

Placing image in a view

I am struggling with the layout in a view. I am trying to place the random picture(in this case starbucks logo) to the left of the blue view. I would like it to be aligned with left border. No matter what layout properties i use for it, it doesn't work as i would expect.
logoStyle:{
width: 210,
height: 120,
left: 0,
resizeMode: 'contain',
backgroundColor: 'blue',
},
Here is how it looks
You want put image logo on the left of background view. Here's example:
render() {
<View style={{flex: 1, backgroundColor: 'blue'}}>
<View style={{ flexDirection: 'row',
alignItems: 'flex-start', justifyContent: 'flex-start'}} >
<Image style={{width: 20, height: 20}} />
</View>
</View>
}
You can try it!
Cheer!