How to use zIndex in react-native

后端 未结 5 1178
攒了一身酷
攒了一身酷 2020-12-23 13:19

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

相关标签:
5条回答
  • 2020-12-23 13:56

    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>
    
    0 讨论(0)
  • 2020-12-23 13:57

    You cannot achieve the desired solution with CSS z-index either, as z-index is only relative to the parent element. So if you have parents A and B with respective children a and b, b's z-index is only relative to other children of B and a's z-index is only relative to other children of A.

    The z-index of A and B are relative to each other if they share the same parent element, but all of the children of one will share the same relative z-index at this level.

    0 讨论(0)
  • 2020-12-23 14:01

    I finally solved this by creating a second object that imitates B.

    My schema now looks like this:

    I now have B1 (within parent of A) and B2 outside of it.

    B1 and B2 are right next to one another, so to the naked eye it looks as if it's just 1 object.

    0 讨论(0)
  • 2020-12-23 14:12

    UPDATE: Supposedly, zIndex has been added to the react-native library. I've been trying to get it to work without success. Check here for details of the fix.

    0 讨论(0)
  • 2020-12-23 14:14

    Use elevation instead of zIndex for android devices

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

    This worked fine for me!

    0 讨论(0)
提交回复
热议问题