Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zIndex not working properly in react-native in IOS

I only have 1 image that needs a zIndex to show it in front. It works on Android but it doesn't show up in IOS devices. Not sure why.

Here is the picture for Android: enter image description here

Here is the picture for IOS: enter image description here

As you can see, the user image doesn't appear in IOS.

Code is like this:

 <View>
      <Image
        source={require("./images/user-img.png")}
        style={{width:95,height:95,marginTop:10,marginLeft:135,position:'absolute',zIndex:1}}
      />
      <ImageBackground
        source={require("./images/bg-img.png")}
        style={{ width: "100%", height: "100%"}}>
        <View>
         //extra code here
        </View>
      </ImageBackground>
 </View>

Anyone know the reason?

like image 665
Joshua Rajandiran Avatar asked Aug 02 '18 07:08

Joshua Rajandiran


People also ask

Does zIndex work in React Native?

To use z-index in React Native, we can use the zIndex style. to add 2 View s that has different zIndex values. The component with the higher zIndex value is laid over the component with the lower zIndex value. So the green square goes on top of the red square.

How does Z-Index work in React Native?

Introduction of React Native zIndex. To specify the stack order of an element, zIndex is used. The element which has a greater stack order is positioned in front of the element which has a low stack order. Z-index always works on the elements which are positioned as absolute, sticky, fixed and relative.

Why we use zIndex 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.


1 Answers

Please add zindex to parent view. In iOS, the zIndex doesn't work for nested parentView. You need to make the parentView has high zIndex, and then target View again.

<View style={{zIndex: 10}}>
  <Image
    source={require("./images/user-img.png")}
    style={{width:95,height:95,marginTop:10,marginLeft:135,position:'absolute',zIndex:20}}
  />
  <ImageBackground
    source={require("./images/bg-img.png")}
    style={{ width: "100%", height: "100%"}}>
    <View>
     //extra code here
    </View>
  </ImageBackground>

like image 128
Ken W. Avatar answered Sep 28 '22 01:09

Ken W.