Weird Margin behavior in React Native in combination with absolute position - react-native

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.

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>

Stack components centered above each other in 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>

React-Native Flexbox - Position One Item at Vertical Center and other at Bottom

Within a parent view, I would like to vertically center align one piece of text and have another at the bottom of the view.Whenever I add the bottom text, it shifts the position of the vertical centered view upwards (to make it the vertical center of the remaining space).
How do I keep the text vertically centered align relative to the parent view?
Update: I understand I can do this using {position: 'absolute',
bottom:0}, but want to understand the flex-box solution.
<View style={{height: 300, borderColor: "black", borderWidth: 1}}>
<View style={{ justifyContent: "center", flex: 1 }}>
<Text>Vert Middle</Text>
</View>
<View>
<Text>Vert Bottom</Text>
</View>
</View>
Just try below code
<View style={{height: 300, borderColor: "balck", borderWidth: 1}}>
<View style={{ backgroundColor: 'red', justifyContent: "center", flex: 1 }}>
<Text>Vert Middle</Text>
</View>
<View style={{position: 'absolute', bottom: 0}}> // Here is your updations
<Text>Vert Bottom</Text>
</View>
</View>
This is going to work for you. Also #Nitish answer is going to work too.
render() {
return (
<View style={{
height: 300,
borderColor: "black",
borderWidth: 1
}}>
<View style={{
justifyContent: "center",
flex: 1
}}>
<Text>Vert Middle</Text>
</View>
<View style={{
height:0, //Here...
justifyContent: "flex-end",
}}>
<Text>Vert Bottom</Text>
</View>
</View>
);
}

React-native two views with image between

I'm new to react-native and I want to do a page that basically has 2 Views and between them has a rounded Image (like in the image) , i know how to do 2 Views but I don't know how to put the Image there correctly.
This should work:
<View style={{flex: 1}}>
<View style={{height: 200, backgroundColor: 'grey'}}></View>
<View style={{flexGrow: 1, backgroundColor: 'black', alignItems: 'center'}}>
<Image
source={require('./icon.png')}
style={{
position: 'absolute',
top: -40,
height: 80,
width: 80}}
/>
</View>
</View>

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!