react-native icon indicating copy to clipboard operation
react-native copied to clipboard

zIndex does not work with dynamic components on Android

Open madox2 opened this issue 10 years ago • 84 comments

I am trying to render elements conditionally where each element has different zIndex style property. Using folowing code in Android emulator with react-native 0.30.0.

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default class Demo extends Component {
  constructor(props, ctx) {
    super(props, ctx);
    this.state = {
      showGreen: true,
    };
  }
  render() {
    return (
      <View style={{flex: 1, padding: 20}}>
        <View style={[styles.item, {zIndex: 3, backgroundColor: 'red'}]}>
          <Text>zIndex: 3</Text>
        </View>
        {this.state.showGreen &&
        <View style={[styles.item, {zIndex: 2, backgroundColor: 'green'}]}>
          <Text>zIndex: 2</Text>
        </View>
        }
        <View style={[styles.item, {zIndex: 1, backgroundColor: 'blue'}]}>
          <Text>zIndex: 1</Text>
        </View>
        <View style={styles.button}>
          <Text onPress={() => this.setState({ showGreen: !this.state.showGreen })}>
            Toggle green
          </Text>
        </View>
      </View>
    );
  }

}

const styles = StyleSheet.create({
  item: {
    marginTop: -20,
    height: 50,
    paddingTop: 22,
  },
  button: {
    backgroundColor: 'gray',
    marginTop: 30,
  }
});

Initial screen looks like expected:

zindex1

When I click 'Toggle green' button to dynamically show/hide green element it hides blue element instead and furthermore element's text is missing:

zindex2

When I click the button again, red and green elements remains visible and the toggle button jumps down.

madox2 avatar Jul 22 '16 10:07 madox2

same here, zIndex + position : absolute, any update ?

Ycfx avatar Feb 21 '17 11:02 Ycfx

Instead of return null I just return <View /> and it works fine for me on both Android and iOS.

frmrgr avatar Feb 26 '17 23:02 frmrgr

any updates on this? I am also experiencing this bug, with zIndex + position : absolute on android

perrosnk avatar Mar 13 '17 22:03 perrosnk

Created product pain: https://react-native.canny.io/feature-requests/p/zindex--positionabsolute-breaks-with-dynamic-components-on-android

If the title and/or post can be clarified, I'm all for it.

coderdave avatar Mar 23 '17 19:03 coderdave

Same issue here 🤣

putuyoga avatar Mar 24 '17 10:03 putuyoga

Experiencing the same issue here, but even with position: relative. I was able to fix this by using a Modal component, but I need the rest of the screen to be touchable, and Modals are fullscreen so I could not use that either. Thanks!

rnowm avatar Mar 27 '17 12:03 rnowm

I am using Modal from react-native and when I close it (set visible to false), it makes my other components disappear one by one. First close navbar, second close a card. Both has elevation and zIndex set.

@rnowm Can you share your fix with Modal?

Gumija avatar Mar 28 '17 08:03 Gumija

Glad I'm not the only one having issues with this (zIndex + position: absolute). I've been trying to statically set zIndex's, but then realized I could just ditch zIndexing my absolute views and just reorder them in my render return so that they stay on top. I know this will not fix most of the problems of those who actually need to use zIndex but hopefully it helps someone.

SteffeyDev avatar Mar 29 '17 06:03 SteffeyDev

@Gumija in my case I just needed one component with z-index (a toast) over my view, maybe that's why it worked for me. Did not tested with other elements using z-index.

rnowm avatar Mar 29 '17 07:03 rnowm

Same issue here

Kalyani518 avatar Mar 29 '17 09:03 Kalyani518

I'm also having an issue with zIndex + position: 'absolute' on Android. I have a dropdown menu component that always displays the currently selected item, and when it is activated, it also displays a list of all of the available dropdown menu items below this. The view that this dropdown menu is used in has other components as siblings of the dropdown menu component, and they are all after the dropdown menu component, since I want the currently selected item of the dropdown menu component to be shown first.

I'm trying to use zIndex so that the list of the available dropdown menu items is shown above the other components that are rendered after the dropdown menu. I'm simply applying a zIndex: 2 to the <View> that contains this list of available dropdown menu items to achieve this. The list of available dropdown menu items is rendered within a <View> that has position: 'absolute'.

This technique is working perfectly on iOS and behaves exactly as I expect/want. However, the exact same code is not working on Android; The list of available dropdown menu items is always rendered behind the other components that are rendered after the dropdown menu, and therefore is not visible. I've tried all sorts of variations of zIndex values for different components in the hierarchy, as well as elevation, and nothing seems to make this work on Android, even though it works fine on iOS.

Something must be different about how zIndex works in iOS vs Android, as the same code behaves differently in these two environments.

jordanmkoncz avatar Apr 05 '17 05:04 jordanmkoncz

Same here.

fastmuse avatar Apr 06 '17 13:04 fastmuse

Problem's still here

smiled0g avatar Apr 09 '17 18:04 smiled0g

same here.

Beelphegor avatar Apr 10 '17 01:04 Beelphegor

Adding "elevation: 100" to the style solved my issue. credit goes to Shukarullah Shah

pengyanb avatar Apr 11 '17 01:04 pengyanb

Same issue here, everything works as expected without a zIndex (It just doesn't look right) Once I add a zIndex of 1 it goes to shit.

JannesV avatar Apr 18 '17 10:04 JannesV

One tip that might be helpful: You might get away with changing the rendering order instead of using zIndex. This only works on elements under the same parent though.

smiled0g avatar Apr 18 '17 11:04 smiled0g

i got same issue on android, add loading component with absolute + zIndex. It will cause unexpected rendering on that screen (such as some components disappear or white out all screen or some components) and stop the app from working. I have tried all solution mention above such add elevation: 100 and move loading component to the last component in the view. But the issue still happens on android.

https://gist.github.com/nhuthuynh/009dee73675c1dbed41d34a86b359057

"react": "^16.0.0-alpha.6",
"react-native": "^0.43.4", Genymotion: 2.9.0 Virtual Device: Google Nexus 5X 1080x1920

nhuthuynh avatar Apr 27 '17 12:04 nhuthuynh

Got the same problem here ! After reading the discussions and skimming through the code I realized that the root problem lies in the current implementation of zIndex which is basically invoking bringToFront() on the child views in order of their zIndex. ( It is done in ViewGroupManager.reorderChildrenByZIndex() method)

This method spoils the natural index of child views in their parent. Therefore indicesToRemove parameter in NativeViewHierarchyManager.manageChildren will no longer refer to the actual child.

@asgvard did a beautiful job in PRs (#10954) trying to use tag-reference instead of child-index. However @astreet suggested the problem is better be solved fundamentally by migrating to a zIndex implementation mechanism that doesn't mess with child-view indices (probably using android's ViewGroup#getChildDrawingOrder).

It seems like 23 days ago @janicduplessis fixed zIndex mechanism in commit 9a51fa8. When I used react-native source from master branch everything was working just fine.

Hope this commit goes on the next release !

nimatrueway avatar Apr 28 '17 20:04 nimatrueway

actually, elevation property worked on android, just remove zIndex property on android platform and make the component with elevation property is the last component.

ios: {
            zIndex: 1
        },
        android: {
            elevation: 1
        },

it works for me! thank all!

nhuthuynh avatar May 02 '17 12:05 nhuthuynh