One of my React Native screens needs to have a TouchableOpacity at the bottom of the screen. I’ve tried setting position: "absolute"
and bottom: 0
, however, this just causes the element to be a few hundred pixels below the screen. How can I make my TouchableOpacity be at the bottom of the screen all the time?
<View style={styles.mainView}>
<View style={{ position: "relative" }}>
<TouchableOpacity style={styles.cartView}>
<Text style={styles.cartText}>View cart</Text>
</ViewTouchableOpacity
</View>
}
/>
</View>
//styles
const styles = StyleSheet.create({
mainView: {
// devDims are the device dimensions
minHeight: devDims.height,
minWidth: devDims.width,
backgroundColor: "white",
paddingLeft: 10,
paddingRight: 10,
paddingTop: 10,
},
cartView: {
justifyContent: "center",
alignItems: "center",
maxHeight: 50,
minWidth: "100%",
alignSelf: "center",
marginTop: 50,
padding: 10,
borderRadius: 20,
},
cartText: {
fontFamily: "semi",
fontSize: 22,
},
});
2
Answers
We could handle this without absolute positioning using
flex
alone.Add
flex:1
to the parent view and to the view that wraps the sticky bottom. Then, addflex: 2
to the view that wraps the other content.Here is a minimal generic component that lets you add a sticky bottom component.
The result looks as follows:
However, we can use
absolute
positioning as well. You are just missing theflex: 1
for the parent view.The result is as follows:
if you are using
position
asabsolute
then your view must be last view before last closed view tagWhen you have declared any view at bottom of the screen then you should be do like this