zIndex 在 IOS 中的 react-native 中无法正常工作

Jos*_*ran 6 javascript z-index ios react-native

我只有 1 张需要zIndex在前面显示的图像。它适用于 Android,但它不会出现在 IOS 设备中。不知道为什么。

这是Android的图片: 在此处输入图片说明

这是IOS的图片: 在此处输入图片说明

如您所见,用户图像没有出现在 IOS 中。

代码是这样的:

 <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>
Run Code Online (Sandbox Code Playgroud)

有谁知道原因吗?

Ken*_* W. 17

请将 zindex 添加到父视图。在 iOS 中,zIndex 不适用于嵌套的 parentView。您需要使 parentView 具有高 zIndex,然后再次定位 View。

<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>
Run Code Online (Sandbox Code Playgroud)

  • 哇哦,这完全没有意义,但却解决了我的问题。React Native 太奇怪了!谢谢! (2认同)
  • 如果你的元素是**可点击的**,你需要稍微调整一下,因为将“zIndex”添加到父级可以修复iOS,但会弄乱android。`&lt;View style={Platform.OS === 'ios' ? {zIndex: 10} : {}}&gt; &lt;Button style={{zIndex: 99,levation: 99}}&gt; 可点击 &lt;/Button&gt;&lt;/View&gt; ` (2认同)

小智 0

尝试先渲染背景图像,然后渲染个人资料图像

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