Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View with absolute position in SafeAreaView in React Native

I need to place a <View> with position: 'absolute' so it can overlay another view below. I want this not to be behind the status bar in iOS, so I've put everything inside a <SafeAreaView>.

Unfortunately, the absolute position seems to be relative to the full screen instead of its parent view (the SafeAreaView).

Is there a trick?

const styles = StyleSheet.create({
    safeAreaView: {
      flex: 1,
    },
    map: {
      flex: 1,
    },
    userLocationButton: {
      position: 'absolute',
      right: 12,
      top: 12,
    },
  });

  return (
    <SafeAreaView style={styles.safeAreaView}>
        <ClusteredMapView style={styles.map} />
      )}
        <TouchableOpacity style={styles.userLocationButton}>
          <Image source={UserLocationButton} />
        </TouchableOpacity>
      )}
    </SafeAreaView>
  );

enter image description here

like image 686
jdanthinne Avatar asked Jan 22 '20 13:01

jdanthinne


2 Answers

I had the same issue, fixed it by wrapping the absolute element with another View.

<SafeAreaView style={{flex: 1}}>
    <View style={{flex: 1}}>
        <TouchableOpacity style={{height: 40, width: 40, borderRadius: 20}} />
    </View>
</SafeAreaView>
like image 186
Oleksandr Karpov Avatar answered Oct 21 '22 07:10

Oleksandr Karpov


add status bar size value to top of absolute styling

get status bar size from this function getStatusBarHeight()

import { Dimensions, Platform, StatusBar } from 'react-native';
function isIphoneX() {
  const dimen = Dimensions.get('window');
  return (
    Platform.OS === 'ios' &&
    !Platform.isPad &&
    !Platform.isTVOS &&
    (dimen.height === 812 ||
      dimen.width === 812 ||
      (dimen.height === 896 || dimen.width === 896))
  );
}
export function getStatusBarHeight(skipAndroid) {
    return Platform.select({
        ios: isIPhoneX() ? 44 : 20,
        android: skipAndroid ? 0 : StatusBar.currentHeight,
        default: 0
    })
}
like image 23
Aurangzaib Rana Avatar answered Oct 21 '22 09:10

Aurangzaib Rana