Adding margin to component changes screen layout react-native - react-native

I'm building a react-native app and I'm having a problem styling the components.
For some reason when I add a margin to one of the components the main component doesn't occupy the full height of the screen even though is "flex:1".
In this example the problem happens with the back button, if I use padding it works fine but I need to use margin so the user doesn't press the wrong component by mistake.
And the same problem happens in the rest of the app with different components that have a margin property.
Main component
body: {
flex: 1,
backgroundColor: 'green'
},
subHeader: {
backgroundColor: '#f4f4f4',
height: 95,
flexDirection: 'row'
},
vericalBar: {
width: 9
},
subHeaderDescription: {
flex: 1,
justifyContent: 'center',
marginLeft: 16
},
subHeaderLocation: {
color: '#2d2d2d',
fontSize: 14,
marginBottom: 10
},
subHeaderText: {
color: '#2d2d2d',
fontSize: 20
},
subHeaderStatus: {
alignItems: 'center',
justifyContent: 'center',
marginRight: 18
},
listDescription: {
backgroundColor: 'red',
flexGrow: 1,
paddingTop: 16,
paddingLeft: 20
},
taskDescription: {
color: '#2d2d2d',
fontSize: 16
}
<View style={localStyles.body}>
<Header navigation={this.props.navigation} />
<View style={localStyles.subHeader}>
<View style={[localStyles.vericalBar, { backgroundColor: task.color }]} />
<View style={localStyles.subHeaderDescription}>
<Text style={localStyles.subHeaderLocation}>{location.name}</Text>
<Text style={localStyles.subHeaderText}>{task.name}</Text>
</View>
{iconSelector(task)}
</View>
<BackButton navigation={this.props.navigation} />
<View style={localStyles.listDescription}>
<Text style={localStyles.taskDescription}>{task.description}</Text>
</View>
{returnStatus(task)}
<ReportIssueButton navigation={this.props.navigation} />
<Footer navigation={this.props.navigation} />
</View>
Back Button
<TouchableWithoutFeedback onPress={() => navigation.goBack()}>
<View style={localSytles.container}>
<Image style={{ width: 14, height: 14 }} source={require('../../Images/Back.png')} />
<Text style={localSytles.buttonText}>Back</Text>
</View>
</TouchableWithoutFeedback>
container: {
flexDirection: 'row',
height: 40,
paddingLeft: 20,
alignItems: 'center',
backgroundColor: 'blue'
},
buttonText: {
color: '#2d2d2d',
fontSize: 14,
marginLeft: 10
}

I've found the problem, the container "listDescription" had a "paddingTop: 16"
by removing that property it allowed the container to push the rest of the containers down.

Related

React Native scrollview stops scrolling after screen change

I have a ScrollView in react native where the functionality stops working only after I change the screen. For example: when I reload and land on the home screen (with the ScrollView) it all works fine. but when I use the bottom tab navigator to change the screen and return back to the home screen, it doesn't work anymore.
Thanks in advance for the help!
const Home = () => {
return (
<SafeAreaView style={{ flex: 1, backgroundColor: COLORS.background }}>
<Header screenName="home" />
<View style={{ color: COLORS.secondary, flex: 14, paddingHorizontal: SIZES.width / 20}}>
<ScrollView
showsVerticalScrollIndicator={false}
>
<Text style={styles.popularHeader}>Title</Text>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
style={styles.popular}
>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
</ScrollView>
<View style={styles.event}>
<Text style={styles.eventTitle}>event title</Text>
<Text style={styles.eventDate}>date</Text>
</View>
</ScrollView>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
popularHeader: {
color: COLORS.text,
fontSize: SIZES.h4,
alignSelf: 'center',
marginBottom: 10
},
popular: {
borderBottomWidth: 1,
paddingBottom: SIZES.height / 30,
marginBottom: SIZES.height / 30,
borderBottomColor: COLORS.secondary
},
popularItem: {
width: 100,
height: 100,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: COLORS.text,
marginRight: 10,
},
event:{
height: SIZES.height / 3,
backgroundColor: COLORS.text,
borderRadius: 5,
marginBottom: SIZES.height / 30,
padding: 10
},
eventTitle: {
fontSize: SIZES.h3,
alignSelf: 'center'
},
eventDate: {
fontSize: SIZES.h6,
alignSelf: 'center'
}
})
In case someone wonders, after a three-day-long search I found out the problem was with the bottomTabNavigator not unmounting the screens after a change of screen. so the solution that fixed my problem is:
in screenOptions:
unmountOnBlur: true

Scroll not working in ScrollView (React Native)

I've seen the same question many times, but none of the solutions I found worked for me.
return part
<View style={styles.root}>
<ScrollView contentContainerStyle={{ flexGrow: 1 }} scrollEnabled={true}>
<View style={styles.dpArea}>
<Image
source={{
uri: "URI GOES HERE",
}}
style={{ width: "100%", height: "100%" }}
resizeMode="contain"
/>
</View>
<View style={styles.nameArea}>
<View style={{ flex: 8, alignItems: "center" }}>
<Text style={styles.nameText}> {nameUpper}</Text>
</View>
<View style={{ flex: 2, paddingLeft: 10 }}>
<MaterialIcons name="edit" size={24} color="gray" />
</View>
</View>
<View style={styles.emailArea}>
<Text
style={{
fontFamily: "Reg",
fontSize: 12,
color: "gray",
marginRight: 4,
}}
>
Logged in using
</Text>
<Text style={{ fontFamily: "Reg", fontSize: 12 }}>{email}</Text>
</View>
<View style={styles.statsArea}>
<StatsContainer text="STAT 1" number="41" />
<StatsContainer text="STAT 2" number="93" />
</View>
<View style={styles.feedbackArea}>
<Text style={{ fontFamily: "Bold", fontSize: 12, color: "gray" }}>
Send feedbacks, feature requests or report bugs:
</Text>
<FeedbackArea content={feedback} />
</View>
<View style={styles.logoutArea}>
<GenButton content="LOGOUT" />
<DeleteAcButton content="DELETE ACCOUNT" />
</View>
<View style={styles.madeByArea}>
<Text style={styles.madebyText}>Developed by:</Text>
</View>
</ScrollView>
</View>
StyleSheet :
root: {
flex: 1,
},
dpArea: {
height: "30%",
marginTop: 20,
overflow: "hidden",
alignItems: "center",
justifyContent: "center",
},
nameArea: {
height: "10%",
flexDirection: "row",
justifyContent: "space-around",
alignItems: "center",
},
emailArea: { height: "10%", flexDirection: "row", justifyContent: "center" },
statsArea: { flexDirection: "row", height: "20%", marginBottom: "10%" },
feedbackArea: { height: "20%", marginHorizontal: "5%" },
logoutArea: {
width: "100%",
justifyContent: "space-around",
alignItems: "center",
height: "30%",
marginVertical: "5%",
},
madeByArea: {
height: "10%",
justifyContent: "flex-end",
alignItems: "center",
marginBottom: 3,
},
madebyText: {
fontFamily: "Reg",
fontSize: 12,
color: "gray",
},
nameText: {
fontFamily: "Bold",
fontSize: 30,
},
What I've tried: making scrollview the parent component, wrapping scrollview in a root view, flex & flexGrow=1 in contentContainerStyle, flex & flexGrow=1 in ScrollView style parameter.
What I've observed is, if I change any text's font size to say 300, then scroll works but not all components are rendered, however, with normal font size, there's no scroll even though the page exceeds the screen size.
I presume it's due to the way stylesheet is configured, but am unable to locate the error.

Text in Button doesn't show text in ios (native base)

hi in my project I use nativeBase. I use <Button><Text>blablabla</Text></Button>
when running in android ok but when run-ios text not showing.
this is my component :
<View style={{ marginLeft: 7, flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
<View style={styles.btncontent}>
<Right style={{ flex: 0.2 }}>
<Image source={mapBtnLogo} style={styles.btnImage} />
</Right>
<Left style={{ flex: 0.8 }}>
<Button full transparent style={styles.btn} onPress={() => props.myProps.gotoMap([section.YPOINT, section.XPOINT])}>
<Text style={styles.btnText}>
نمایش روی نقشه
</Text>
</Button>
</Left>
</View>
</View>
and this is styles :
const styles = StyleSheet.create({
...Platform.select({
ios: {
btnImage: {
margin: 5,
marginRight: 20,
width: moderateScale(20),
height: moderateScale(20),
},
btn: {
//alignItems: 'flex-start',
width: '100%',
height: '10%',
},
btnText: {
fontFamily: 'IRANSansMobile',
fontSize: 15,
color: '#000',
},
}, android: {
btn: {
width: '100%',
height: '10%',
},
btnText: {
fontFamily: 'IRANSansMobile',
fontSize: 15,
color: '#000000'
},
btncontent: {
backgroundColor: '#59c5b8',
flexDirection: 'row-reverse',
alignItems: 'center',
borderWidth: 0.3,
borderRadius: 5,
marginLeft: 10,
width: '95%',
},
}
remove node module and install again not working.
and clear Xcode and run again but not working.
I read this post NativeBase Button doesn't show text but I don't use padding and still not showing text
You can try TouchableOpacity.
<Left style={{ flex: 0.8 }}>
<TouchableOpacity style={styles.btn} onPress={() => props.myProps.gotoMap([section.YPOINT, section.XPOINT])}>
<Text style={styles.btnText}>
نمایش روی نقشه
</Text>
</TouchableOpacity>
</Left>
I found my mistake in styling :
change height style btn in the stylesheet from 10% to 100% !!
thank

React Native; How to place spaces in FlatList

I am trying to build a flat list in react native but am having trouble keeping the specific list items from not touching. I tried to use the itemSeparatorComponent but the problem still persisted.
This is my FlatList component
<View style={{ position: "absolute", bottom: -35, marginLeft: 40 }}>
<FlatList
horizontal
data={this.state.hitRecords}
renderItem={({ item }) => (
<ArticleBox
title={item.title}
year={item.year}
time={item.time}
author={item.author}
/>
)}
itemSeparatorComponent = {()=> </View>}
/>
</View>
This is my ArticleBox component
const styles = StyleSheet.create({
box: {
backgroundColor: "rgba(0.0,0.0,0.0,0.9)",
height: 150,
width: 300,
alignSelf: "center",
flex: 2,
flexDirection: "column",
borderRadius: 25,
elevation: 2
},
layout: {
textAlign: "center",
color: "#FFF"
},
input: {
color: "#ff8c00"
}
});
class ArticleBox extends Component<Props> {
render() {
return (
<View style={styles.box}>
<Text style={styles.layout}>
Time: <Text styles={styles.input}>{this.props.time}</Text>
</Text>
<Text />
<Text style={styles.layout}>Publication Year: {this.props.year}</Text>
<Text />
<Text style={styles.layout} numberOfLines = {3} >Publication Title: {this.props.title}</Text>
<Text />
<Text style={styles.layout}>Author: {this.props.author}</Text>
</View>
);
}
}
I am trying to get the list to behave like this
item item item item
but the list behaves like this
itemitemitemitem
Add marginLeft: 12 in your 'ArticleBox' Component. like this.
box: {
backgroundColor: "rgba(0.0,0.0,0.0,0.9)",
height: 150,
width: 300,
alignSelf: "center",
flex: 2,
flexDirection: "column",
borderRadius: 25,
elevation: 2,
marginLeft: 12
}

How to have elements automatically relocate themselves when one element changes its size in react native?

I currently have a multiline textInput that automatically expands and a button below it. The problem is that the button's location seems to be fixed, it doesn't move down and textInput would just run over it.
Everything is wrapped in a KeyboardAvoidingView:
<KeyboardAvoidingView
style={styles.container}
behavior="padding">
<StatusBar barStyle="light-content"/>
<Text style={styles.title}>{this.state.form.prompt}</Text>
<View style={styles.simpleContainer}>
{this._renderForms()}
<View style={[styles.forwardButtonLocation, styles.indentLeft]}>
<ForwardButton
onButtonPress={this._submitInfo}
/>
</View>
</View>
<View style={{flex: 1, justifyContent: 'flex-end', alignItems: 'flex-end',}}>
<SkipButton
onButtonPress={this._nextState}
/>
</View>
</KeyboardAvoidingView>
this._renderForms() renders the corresponding form for each state, in this case, it renders:
<View style={styles.simpleContainer}>
<AutoExpandingTextInput
style={styles.textContentDark}
placeholder={this.state.form.userInput}
placeholderTextColor={DARK_THEME_CONTENT_COLOR_SECONDARY}
onChangeText={(userInputValue) =>
//TODO: Saving text to nested userInput is causing problem, temporarily save it to userInputValue
//this.setState({form: {...this.state.form, userInput: text}}
this.setState({userInputValue}
)}
/>
</View>
Here is my style sheet:
const styles = StyleSheet.create({
simpleContainer: {
flex:1,
justifyContent: 'center',
},
container: {
flex: 1,
justifyContent: 'space-between',
backgroundColor: THEME_COLOR,
},
title: {
flex: 1,
marginTop: '15%',
marginLeft: '3%',
fontWeight: 'bold',
fontSize: TITLE,
color: DARK_THEME_CONTENT_COLOR_PRIMARY,
},
textContentDimmed: {
fontSize: TITLE_SECONDARY,
color: DARK_THEME_CONTENT_COLOR_SECONDARY,
},
textContentWhite: {
fontSize: TITLE_SECONDARY,
color: DARK_THEME_CONTENT_COLOR_PRIMARY,
},
textContentDark: {
fontSize: TITLE_SECONDARY,
color: LIGHT_THEME_CONTENT_COLOR_SECONDARY,
},
indentLeft: {
marginLeft:'10%',
},
forwardButtonLocation: {
flex: 1,
justifyContent: 'flex-start',
},
whiteLine: {
width: '100%',
height: 1,
marginTop: 4,
marginHorizontal: '6%',
backgroundColor: DARK_THEME_CONTENT_COLOR_PRIMARY,
},
});
Is there any way to automatically relocate the button when the size of the textInput changes?