I've want to achieve the following:
The following images are what I can do right now, but that's NOT what I want.
Sample of code I have right now:
renderA() { return ( <View style={ position: 'absolute', zIndex: 0 }> // parent of A <View style={ zIndex: 2 }> // element A </View> <View style={ zIndex: 2 }> // element A </View> </View> ); } renderB() { return ( <View style={ position: 'absolute', zIndex: 1 }> // element B </View> ); } render() { return ( <View> {this.renderA()} {this.renderB()} </View> ); }
To put it in words, I want
Note that Parent A and Element B both have to be absolutely positioned, and both elements A and elements B have to be clickable...!
The zIndex is used to specify the stack order of an element in react native applications. The elements can be placed in front of the other elements by giving them the higher value of the zIndex . If we want to display an element between 2 elements, we can assign a larger zIndex value.
zIndex is the Expo and React Native analog of CSS's z-index property which lets the developer control the order in which components are displayed over one another.
To set the z-index attribute on an element in React, set the style prop on the element and use the zIndex property, e.g. style={{zIndex: '3'}} . The z-index CSS property influences the way HTML elements stack on top of one another. Copied!
Use elevation
instead of zIndex
for android devices
elevatedElement: { zIndex: 3, // works on ios elevation: 3, // works on android }
This worked fine for me!
I believe there are different ways to do this based on what you need exactly, but one way would be to just put both Elements A and B inside Parent A.
<View style={{ position: 'absolute' }}> // parent of A <View style={{ zIndex: 1 }} /> // element A <View style={{ zIndex: 1 }} /> // element A <View style={{ zIndex: 0, position: 'absolute' }} /> // element B </View>
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