Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use zIndex in react-native

I've want to achieve the following:

enter image description here

The following images are what I can do right now, but that's NOT what I want. scenarioAscenarioB

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

  • Parent A: to be below everything.
  • Element B: to be at the above parent A but below element A.
  • Element A: above everything.

Note that Parent A and Element B both have to be absolutely positioned, and both elements A and elements B have to be clickable...!

like image 553
SudoPlz Avatar asked Jan 30 '17 18:01

SudoPlz


People also ask

How does zIndex work in React Native?

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.

Is there Z-index in React Native?

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.

How do you use zIndex in react JS?

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!


2 Answers

Use elevation instead of zIndex for android devices

elevatedElement: {   zIndex: 3, // works on ios   elevation: 3, // works on android } 

This worked fine for me!

like image 66
Suleiman AbdulMajeed Avatar answered Nov 15 '22 14:11

Suleiman AbdulMajeed


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> 
like image 35
Matt Aft Avatar answered Nov 15 '22 14:11

Matt Aft