In React Native, how do I put a view on top of another view, with part of it lying outside the bounds of the view behind?

前端 未结 7 849
一向
一向 2020-12-23 18:50

I\'m trying to make a layout as per below with React Native.

\"enter

How do I

相关标签:
7条回答
  • 2020-12-23 19:19

    The above solutions were not working for me. I solved it by creating a View with the same background colour as the parent and added negative margin to move the image upwards.

    <ScrollView style={{ backgroundColor: 'blue' }}>
      <View
        style={{
          width: '95%',
          paddingLeft: '5%',
          marginTop: 80,
          height: 800,
        }}>
        <View style={{ backgroundColor: 'white' }}>
    
          <Thumbnail square large source={{uri: uri}} style={{ marginTop: -30 }}/>
          <Text>Some Text</Text>
        </View>
      </View>
    </ScrollView>
    

    and I got the following result.

    0 讨论(0)
  • 2020-12-23 19:21

    You can use react-native-view-overflow plugin for placing a view on top of another. It works like the CSS z-index property.

    import ViewOverflow from 'react-native-view-overflow';
    
    <ViewOverflow />
        <View style={[styles2.cardBox, { marginTop: 50 }]}>
        <View style={[styles2.cardItem]} >
          <Text style={styles2.cardHeader}>userList</Text>
        </View>
          <View style={[styles2.cardContent]}>
            <Text style={styles2.text}>overflow: "visible"</Text>
          </View>
          <View style={[styles2.cardItemFooter]} >
            <Text style={styles2.cardTextFooter}>...</Text>
          </View>
        </View>
      </ViewOverflow>
    
    const styles2 = StyleSheet.create({
      cardBox: {
        borderLeftWidth: 0,
        borderTopWidth: 0,
        backgroundColor: "transparent",
        borderWidth: 1,
        borderColor: "#d0d0d0",
        width: '94%',
        alignSelf: 'center',
        height: 200,
        position: "relative",
        borderRadius: 15,
        overflow: "visible" // doesn't do anything
      },
      cardContent: {
        textAlign: "right",
        backgroundColor: "transparent",
        marginTop: 15,
        alignSelf: 'flex-end',
        padding: 5,
      },
      cardHeader: {
        color: '#fff',
        fontFamily: 'Vazir',
        fontSize: 12
      },
      cardItem: {
        backgroundColor: "#3c4252",
        borderRadius: 3,
        position: "absolute",
        top: -10,
        right: -5,
        width: 50,
        height: 20,
        paddingRight: 5,
      },
    })
    
    0 讨论(0)
  • 2020-12-23 19:22

    You can use this OverlayContainer. The trick is to use absolute with 100% size. Check below an example:

    // @flow
    
    import React from 'react'
    import { View, StyleSheet } from 'react-native'
    
    type Props = {
      behind: React.Component,
      front: React.Component,
      under: React.Component
    }
    
    // Show something on top of other
    export default class OverlayContainer extends React.Component<Props> {
      render() {
        const { behind, front, under } = this.props
    
        return (
          <View style={styles.container}>
            <View style={styles.center}>
              <View style={styles.behind}>
                {behind}
              </View>
              {front}
            </View>
            {under}
          </View>
        )
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        alignItems: 'center',
        height: '100%',
        justifyContent: 'center',
      },
      center: {
        width: '100%',
        height: '100%',
        alignItems: 'center',
        justifyContent: 'center',
      },
      behind: {
        alignItems: 'center',
        justifyContent: 'center',
        position: 'absolute',
        left: 0,
        top: 0,
        width: '100%',
        height: '100%'
      }
    })
    
    0 讨论(0)
  • 2020-12-23 19:25

    The easiest way to achieve this is with a negative margin.

    const deviceWidth = RN.Dimensions.get('window').width
    
    a: {
      alignItems: 'center',
      backgroundColor: 'blue',
      width: deviceWidth,
    },
    b: {
      marginTop: -16,
      marginStart: 20,
    },
    
    
    0 讨论(0)
  • 2020-12-23 19:32

    add the following to the "floating" view

    position: 'absolute'
    

    you may also need to add a top and left value for positioning

    see this example: https://rnplay.org/apps/OjzcxQ/edit

    0 讨论(0)
  • 2020-12-23 19:32

    You can use zIndex for placing a view on top of another. It works like the CSS z-index property - components with a larger zIndex will render on top.

    You can refer: Layout Props

    Snippet:

        <ScrollView>
              <StatusBar backgroundColor="black" barStyle="light-content" />
              <Image style={styles.headerImage} source={{ uri: "http://www.artwallpaperhi.com/thumbnails/detail/20140814/cityscapes%20buildings%20hong%20kong_www.artwallpaperhi.com_18.jpg" }}>
                <View style={styles.back}>
                  <TouchableOpacity>
                    <Icons name="arrow-back" size={25} color="#ffffff" />
                  </TouchableOpacity>
                </View>
                <Image style={styles.subHeaderImage} borderRadius={55} source={{ uri: "https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Albert_Einstein_1947.jpg/220px-Albert_Einstein_1947.jpg" }} />
              </Image>
    
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            backgroundColor: "white"
        },
        headerImage: {
            height: height(150),
            width: deviceWidth
        },
        subHeaderImage: {
            height: 110,
            width: 110,
            marginTop: height(35),
            marginLeft: width(25),
            borderColor: "white",
            borderWidth: 2,
            zIndex: 5
        },
    
    0 讨论(0)
提交回复
热议问题