React Native - View disappears when position is absolute on Android

前端 未结 6 1358
温柔的废话
温柔的废话 2021-02-14 08:04

Whenever I made the position style \'absolute\' for this view, it disappears. I have no idea why it is happening.

import React, { Component } from \'react\';
imp         


        
相关标签:
6条回答
  • 2021-02-14 08:19

    I've just experienced this problem and it seems like specifying minWidth and minHeight for the parent of 'absolute' positioned elements solves the problem.

    EDIT: Actually specifying width and height also solves it.

    Here is the updated snack. See line 11.

    0 讨论(0)
  • 2021-02-14 08:27

    Add zIndex:100 to your attributes. Worked for me.

    0 讨论(0)
  • 2021-02-14 08:29

    The problem is the width and height of the TouchableOpacity are zero! because the contents of that are absolute, so you need to set (left, right, top, bottom) for one of the children or set the width and the height for the TouchableOpacity itself

    Why it works in iOS?
    because in iOS UIView doesn't clip its children by default, so actually TouchableOpacity's width and height are still zero but the children overflowed and you can see them, there is a flag for that (Docs)

    but in android ViewGroup always clips its children

    How to solve problems like this?
    there are some tools that you can debug UI at runtime, but I think the easiest way in react-native is set backgroundColor from the root view to the target view!

    so for example in this case: first set backgroundColor for mainView then if it does show, set backgroundColor for parentView and then for TouchableOpacity and you can see it doesn't show backgroundColor of TouchableOpacity because the width and height of that are zero, in each step you can set width and height manually to ensure about the problem

    0 讨论(0)
  • 2021-02-14 08:30

    It looks like z-index works from top to bottom.

    Without setting the zIndex manually, your absolutely positioned component should be lower in order of appearance/line number in your code for it to layer over the content below it.

    so

    <Underlay/>

    then

    <AbsolutelyPositioned/>

    and not the other way around.

    0 讨论(0)
  • 2021-02-14 08:32

    I solved mine by giving parent component a flex:

    position:'relative',
    flex:1
    

    Just dropping it soo it could be helpful to people.

    0 讨论(0)
  • 2021-02-14 08:34

    I cannot comment because of I don't have enough rep, However, additional to what Amir Khorsandi said, you can also use onLayout to see what value you are getting. This way you can see it in your remote console log, even the x and y of the said View. I hope it helps.

    <View onLayout={(event) => {
                    var {x, y, width, height} = event.nativeEvent.layout; console.log(x, y, width, height)}}>
    
    0 讨论(0)
提交回复
热议问题