Is it possible to place one <View> over another in react-native?

和自甴很熟 提交于 2021-02-08 04:30:57

问题


I want to place one <View> over another in react-native. Here is my code:

<View style={styles.container}>
  <View style={styles.vimgProduct}>
    <Image style={styles.imgProduct} source={{uri: "http://www.unclesamsjamms.com/unclesamcash7.jpg"}}/>
  </View>
  <View style={styles.vNewView}>
  </View>
</View>`

and here is my style:

container: {
        flex: 1,
        paddingTop: 65,
        backgroundColor: "#F5FCFF"
},
vimgProduct: {
        paddingTop: 20,
        justifyContent: 'center',
        alignItems: 'center'
},
vNewView: {
        height: 400,
        paddingVertical: 20,
        marginTop: -60,
        marginHorizontal: 40,
        borderWidth: 3,
        borderColor: "black",
        borderRadius: 15,
        flexDirection: "row",
        justifyContent: 'center',
        alignItems: 'center'
}

My problem now is, that the View with Image is over my "vNewView" but the border of "vNewView" is in foreground and overlaps my "vimgProduct".

I want that my vNewView is in background and my vimgProduct is in foreground. How can I achieve this? In HTML/CSS it works with zIndex I think.

Could you help me please here? Would be great!

best regards, Alban


回答1:


To achieve similar functionality to z-index in react native, you simply need to place your components in the order that they are intended to be layered. For example, if you have three components in a row, then the second will overlay the first, and the third will overlay the second, so on and so forth.

For what you are doing, maybe try placing the vNewView as the first item in the view and use position: 'absolute' .




回答2:


I'd here is an overlay container that you can use to overlay your views:

import React, { ReactElement } from 'react';
import { View, StyleSheet, ViewStyle, StyleProp } from 'react-native';

type OverlayContainerProps = {
  back?: ReactElement;
  front?: ReactElement;
  backViewStyle?: StyleProp<ViewStyle>;
  frontViewStyle?: StyleProp<ViewStyle>;
  containerStyle?: StyleProp<ViewStyle>;
};

export default function OverlayContainer({
  back,
  front,
  backViewStyle,
  frontViewStyle,
  containerStyle,
}: OverlayContainerProps): ReactElement {
  return (
    <View style={[styles.container, containerStyle]}>
      <View style={backViewStyle}>{back}</View>
      <View style={[styles.front, frontViewStyle]}>{front}</View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignContent: 'center',
    alignItems: 'center',
    justifyContent: 'center',
  },
  front: {
    position: 'absolute',
    zIndex: 1,
  },
});

Credit where it is due, I got my inspiration for this approach from seeing this bit of code: https://github.com/onmyway133/blog/issues/254



来源:https://stackoverflow.com/questions/37456280/is-it-possible-to-place-one-view-over-another-in-react-native

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!