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>
);
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>
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
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With