Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - View disappears when position is absolute on Android

Tags:

react-native

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';
import { Text, View, Image, TouchableOpacity, StyleSheet } from 'react-native';

export default class App extends Component {
  render() {
    return (
      <View style={styles.mainView}>
        <View
          style={styles.parentView}>
          <TouchableOpacity
            activeOpacity={0.9}
            onPress={() => {}}>
            <View
              style={ [{position: 'absolute'}, styles.textView] }>
              <Text style={styles.textViewText}>
                Just Another Cat
              </Text>
            </View>
            <Image
              style={[{position: 'absolute'}, styles.imageView]}
              source={{ uri: 'https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg' }}
            />
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  mainView: { flexDirection: 'row', flex: 1 },
  parentView: { alignSelf: 'center', flex: 0.5, left: 10, elevation: 3, alignItems: 'flex-start' },
  textView: { width: 250, height: 250, opacity: 1, borderRadius: 125, backgroundColor: '#aaa034' },
  textViewText: { textAlign: 'center', padding: 10, color: '#ffffff', fontSize: 18, top:0, right:0, bottom:0, left:0, textAlignVertical: 'center', position: 'absolute' },
  imageView: { width: 250, height: 250, borderRadius: 125, borderColor: '#000000', borderWidth: 0.2 }
});

When I remove the inline-style: (position: 'absolute') added alongside styles.textView and styles.imageView, they both will be visible again. But I want to make the position absolute for some reason, but then they just disappear. I don't know why. Can anyone explain this?

EDIT: It works in iOS. It's giving this bug on android. Snack URL: https://snack.expo.io/Hy_oRrvOM

like image 457
Ketan Malhotra Avatar asked Mar 01 '18 14:03

Ketan Malhotra


4 Answers

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

like image 110
Amir Khorsandi Avatar answered Oct 16 '22 16:10

Amir Khorsandi


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.

like image 44
Mayowa Daniel Avatar answered Sep 22 '22 08:09

Mayowa Daniel


Add zIndex:100 to your attributes. Worked for me. Not exactly, you just have to put a high enough value for your zIndex param, to put it on top of others.

like image 5
mad_greasemonkey Avatar answered Oct 16 '22 15:10

mad_greasemonkey


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.

like image 2
Sait Banazili Avatar answered Oct 16 '22 14:10

Sait Banazili