Issue
I'm new to React Native and trying to learn how to correctly position components amongst each other. Currently, I made a modal I was wanting for it to appear on top of whatever is in the background. If I take the view that makes the white box, it does appear in the middle.
My Screen I'm wanting to render everything:
<View
style={Styles.background}>
<View
style={{justifyContent:'flex-start', alignItems:'center'}}
>
<View
style={{
backgroundColor:Colors.gtechWhite,
width:'80%',
height:'25%'
}}
>
</View>
</View>
<View
style={{flex:1, justifyContent:'center', alignItems:'center'}} >
{addExerciseModalOpen && <AddExerciseModal setAddExerciseModalOpen={setAddExerciseModalOpen}/>}
</View>
<View style={{ position:'absolute', right:'6%', bottom:'14%', alignItems:'flex-end'}} >
{filterModalOpen && <FilterModal selectedFilter={selectedFilter} setSelectedFilter={setSelectedFilter} />}
</View>
<View style={{position:'absolute', right:'6%', bottom:'8%', alignItems:'flex-end', flexDirection:'row'}}>
<Pressable
style={{paddingRight:35}}
onPress={()=>{
Haptics.notificationAsync(
Haptics.NotificationFeedbackType.Success
)
setAddExerciseModalOpen(false)
setFilterModalOpen(!filterModalOpen);
}}
>
<Octicons name="filter" size={40} color={Colors.darkMode.button} />
</Pressable>
<Pressable
onPress={()=>{
Haptics.notificationAsync(
Haptics.NotificationFeedbackType.Success
)
setFilterModalOpen(false)
setAddExerciseModalOpen(!addExerciseModalOpen);
}}
>
<Ionicons name="add-circle-outline" size={40} color={Colors.darkMode.button}/>
</Pressable>
</View>
</View>
The Component I'm wanting to render in the middle:
<View style={{
flexDirection:'row',
position: 'absolute',
justifyContent: 'flex-start',
alignItems: 'flex-start',
width: '80%',
height: '34%',
borderRadius: 25,
backgroundColor: Colors.darkMode.cards1,
overflow: 'hidden' }}>
<View style={{flex:1, top:10}}>
<View style={{ alignItems: 'center', paddingBottom:25}} >
<Text style={Styles.modalTitleText}>Add An Exercise</Text>
<TextInput
placeholder="Exercise Name"
placeholderTextColor="#ffffff"
style={{
color:'white',
fontFamily:'RobotoSlab-Bold',
fontSize:15,
marginLeft:25,
width:'90%',
marginTop:25,
height:45}}
/>
<WhiteDivider dividerWidth={90} dividerMarginVertical={0}/>
<View style={{
flexDirection:'row',
flexGrow:1,
paddingTop:25,
marginRight:15,
marginLeft:15,
}}>
<ScrollView
style={{width:'100%'}}
horizontal={true}
showsHorizontalScrollIndicator={false}
>
{ExerciseCategories()}
</ScrollView>
</View>
<View style={{
flexDirection:'row',
flexGrow:1,
paddingTop:15,
marginRight:15,
marginLeft:15,
}}>
<Pressable
style={{alignItems:'center', backgroundColor:Colors.gtechWhiteTransparent, borderRadius:9, width:'95%', paddingVertical:4 }}
onPress={()=>setAddExerciseModalOpen(false)}
>
<Text style={{
color: Colors.gtechGold,
fontSize: 17,
fontFamily: 'RobotoSlab-Medium'}}
>
CANCEL
</Text>
</Pressable>
</View>
<View style={{
flexDirection:'row',
flexGrow:1,
paddingTop:15,
marginRight:15,
marginLeft:15,
}}>
<Pressable
style={{alignItems:'center', backgroundColor:Colors.gtechGoldTransparent, borderRadius:9, width:'95%', paddingVertical:4 }}
onPress={()=>setAddExerciseModalOpen(false)}
>
<Text style={{
color: Colors.gtechGold,
fontSize: 17,
fontFamily: 'RobotoSlab-Medium'}}
>
SAVE EXERCISE
</Text>
</Pressable>
</View>
</View>
</View>
</View>
Solution
By using this you can bring Modal to the center.
style:{
flex:1,
justifyContent: 'center',
alignItems: 'center'
}
Example:
import React from "react";
import { View, Text, StyleSheet, Modal } from "react-native";
export default function App() {
return (
<Modal
animationType={"slide"}
transparent={true}
visible={true}
onRequestClose={this.closeModal}>
<View style={styles.centered}>
<View style={styles.childStyle}>
<Text style={styles.title}>Center a View Component</Text>
<Text style={styles.subtitle}>Using Flexbox</Text>
</View>
</View>
</Modal>
);
}
const styles = StyleSheet.create({
centered: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#ffc2c2",
},
title: {
fontSize: 18,
marginVertical: 2,
},
subtitle: {
fontSize: 14,
color: "#888",
},
});
Answered By - Shivo'ham
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.