TouchableOpacity is acting weird inside mapview in React Native - ios

So i have 2 problems with TouchableOpacities inside my MapView:
The text and Icon inside my touchableopacities go outside of the touchableopacity, and in other components that doesn't happen.
One of the touchableopacities does nothing when clicked, it doesn't even change the opacity of the touchableopacity.
I haven't tried anything yet, except for revising my state and how it acts, and affects the elements, but nothing appears to be wrong.
this is my state:
this.state = {
lats: this.tempVars.homelat,
longs: this.tempVars.homelong,
latDs: [0.04],
longDs: [0.05],
numStores: this.tempVars.numStores,
StoreLats: this.tempVars.allLats,
StoreLongs: this.tempVars.allLongs,
StoreNames: this.tempVars.allNames,
createMarker: false,
placeMarker: false,
removeAmarker: false,
}
Helper Methods:
locChooser(){
if(this.state.createMarker){
console.log('Permanent Loc: '+JSON.stringify(this.MapLocs.permanentLoc));
return (
<Image style={{width: 65, height: 100, alignSelf: 'center', marginTop: Math.round(Dimensions.get('window').height/2)-150}} source={require('C:/Users/youse/FetcherApp/app/ping.png')}/>
);
}else if(this.state.placeMarker){
return (<Marker coordinate={this.MapLocs.permanentLoc} onPress={() => {if(this.state.removeAmarker){this.setState({placeMarker: false, removeAmarker: false})}}} image={{uri: 'https://i.pinimg.com/originals/30/98/49/309849c5815761081926477e5e872f1e.png'}}/>);
}else{
return null;
}
}
createHelpers(){
var bigarr = new Array(3);
if(this.state.createMarker){
bigarr[0] =
<View style={{flexDirection: 'column', justifyContent: 'space-between'}}>
<TouchableOpacity style={styles.DrawerOpener} onPress={() => this.props.navigation.dispatch(DrawerActions.openDrawer())}><Icon style={{marginTop: 20, marginLeft: 10}} name='reorder'/></TouchableOpacity>
<TouchableOpacity style={[styles.next, {borderColor: '#000000', backgroundColor: '#000000', marginLeft: 10}]} onPress={() => {this.setState({createMarker: false, placeMarker: true})}}><Text style={{color: '#fff', fontSize: 15}}>Select this Location</Text></TouchableOpacity>
</View>;
return bigarr[0];
}else{
bigarr[1] = <View key={0} style={{flexDirection: 'row', justifyContent: 'space-between'}}>
<TouchableOpacity style={styles.DrawerOpener} onPress={() => this.props.navigation.dispatch(DrawerActions.openDrawer())}><Icon style={{marginTop: 20, marginLeft: 10}} name='reorder'/></TouchableOpacity>
<TouchableOpacity style={[styles.next, {borderColor: '#000000', backgroundColor: '#000000', marginLeft: 10}]} onPress={() => {this.setState({createMarker: true})}}><Text style={{color: '#fff', fontSize: 15}}>Choose a location to order from</Text></TouchableOpacity>
</View>;
bigarr[2] =
<View key={1}>
<TouchableOpacity style={[styles.next, {borderColor: this.BtnColor(!this.state.placeMarker), backgroundColor: this.BtnColor(!this.state.placeMarker), marginLeft: 10}]} disabled={!this.state.placeMarker} onPress={() => {console.log('Placemarker: '+this.state.placeMarker+', removeMarker: '+this.state.removeAmarker+', createmarker: '+this.state.createMarker);this.setState({removeAmarker: true})}}><Text style={{color: '#fff', fontSize: 15}}>Remove a Marker</Text></TouchableOpacity>
</View>;
return [bigarr[1], bigarr[2]];
}
}
And the render method:
render(){
return (
<MapView style={{flex: 2}} onRegionChange={this.__ChangeRegion} showsUserLocation={true} style={{position: 'absolute', left: 0, bottom: 0, right: 0, top: 0}} initialRegion={this.MapLocs.startloc}>
<View>
{this.createHelpers()}
</View>
{this.locChooser()}
{this.AllStores()}
<Marker image={require('C:/Users/youse/FetcherApp/app/home.png')} coordinate={this.MapLocs.homeLoc}/>
{this.displayCars(1)}
</MapView>
)
}

So I figured it out, it appears that components act very abnormally inside a Mapview, however when i nested it inside a View with a width and height of the screen dimensions, and put the components outside of the Mapview they acted normal again and they stayed in their styling and formatting.

Related

React Native - Scrollview scrolls up with many inputs [iOS]

I have many inputs within ScrollView. Everytime I try to tap the Save button, if the last input focused is out of the screen view (where the Save button is), the screen scrolls automatically up to the input. However, if I scroll down again and press/tap the Save button again, it works. So I have to tap twice the button.
This only happens on iOS devices. On Android works perfectly.
Here is my code:
render() {
const keyboardVerticalOffset = Platform.OS === "ios" ? 40 : 0;
return (
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : ""}
keyboardVerticalOffset={keyboardVerticalOffset}
style={styles.container}
>
<View
style={{
flexDirection: "column",
justifyContent: "space-between",
height: "100%",
backgroundColor: colors.white,
}}
>
<ScrollView
ref={(scroll) => (this.scroll = scroll)}
keyboardShouldPersistTaps={'handled'}
style={{
marginBottom: this.state.marginBottom,
paddingHorizontal: 20,
}}
>
<DateForm
onChangeDate={this.onChangeDate.bind(this)}
date={this.state.measureDate}
birthdate={this.state.baby.Birthdate}
/>
<MeasureForm
onChangeMeasure={this.onChangeValue.bind(this)}
value={this.state.value}
scrollTo={this.scrollTo.bind(this)}
/>
<ResultsForm
IC={this.state.indexes.IC}
IABC={this.state.indexes.IABC}
colorIC={this.state.indexes.colorIC}
colorIABC={this.state.indexes.colorIABC}
/>
<CBButton
title={global.loc.getTranslation("AddMeasureSaveButton")}
style={{
width: "100%",
marginHorizontal: 0,
flexGrow: 1,
marginTop: 20,
marginBottom: 50,
}}
disabled={!this.state.validForm}
onPress={() => this.disableButton()}
/>
</ScrollView>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
UPDATE
Here is a minimal reproducible example on snack

KeyboardAvoidingView not working properly in iOS

I'm having trouble getting my KeyboardAvoidingView to register properly on the iOS version of my react-native app. It's nested within another view in my ListFooterComponent of a Flatlist. The behavior is relatively normal on Android, but is not working at all on iOS.
Things I've tried:
Changing the behavior prop (all 3 props didn't end up working)
Changing the keyboardVerticalOffset prop (no amount of offset managed to change anything, it just adds a bunch of padding to the bottom of the screen)
Moving my KeyboardAvoidingView tag to the outermost view in ListFooterComponent. No difference
Adding flex: 1 to inner and outer components
I have seen other posts recommending using other keyboard avoidance libraries, but since this is an Expo Managed project, I don't think these will work for me. Any advice for how to accomplish this using the KeyboardAvoidingView component only?
This is a screenshot of the screen without a keyboard on iOS: this
And this is a screenshot of with a keyboard on iOS: this
And here's the code:
<SafeAreaView style={{ flex: 1 }}>
<FlatList
data={commentData}
style={{ flex: 1 }}
keyboardDismissMode={'on-drag'}
keyboardShouldPersistTaps={'always'}
keyExtractor={item => (item.id)}
renderItem={({ item }) => {
return (
<>
</>
)
}}
ListFooterComponent={() => {
return (
<>
<View style={{ borderColor: "#D6DCE8", marginBottom: 0, borderTopWidth: 2, marginTop: 25, shadowColor: "#000", shadowRadius: 2, shadowOpacity: 0.25, shadowOffset: { width: 0, height: 2 }, elevation: 5 }} />
<View style={{ flex: 1, flexDirection: 'row', paddingTop: 15, paddingBottom: 15 }}>
<TouchableOpacity delayPressIn={20}>
<View style={styles.commentImage}>
<Image source={{ uri: myProfilePicture }} style={styles.image}></Image>
</View>
</TouchableOpacity>
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
keyboardVerticalOffset={Platform.select({ ios: 100, android: 500 })}
style={{ flex: 1, backgroundColor: '#ECECEC', borderRadius: 10, flex: 0.95, paddingVertical: 5, marginLeft: 10 }}>
<TextInput style={{
fontSize: 14,
fontWeight: '500',
paddingHorizontal: 10,
marginHorizontal: 10,
paddingVertical: 6,
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap'
}}
ref={commentRef}
multiline={true}
blurOnSubmit={true}
numberOfLines={2}
onFocus={() => commentRef.current = true}
onBlur={() => commentRef.current = false}
placeholder="Leave a comment..."
defaultValue={comment}
onChangeText={(newValue) => { comment = newValue }}
onSubmitEditing={() => submitComment()} />
</KeyboardAvoidingView>
</View>
<View style={Platform.OS === 'android' ? { marginBottom: 200 } : { marginBottom: 100 }} />
</>
)
}}
ListHeaderComponent={() => {
return (
<>
</>
)
}}
/>
</SafeAreaView>
For the above layout, use KeyboardAvoidingView above FlatList in your hierarchy, and place the comment box not as the footer, but outside of the list component altogether.
Here's a snack with these changes (OP's code simplified for context): https://snack.expo.dev/#mlisik/thankful-tortilla
A few general notes:
padding behaviour should be what you want
keyboarVerticalOffset is useful if your view is nested inside a navigator (eg. from react-navigation), and navigation bar or tabbar are visible - you should then set the value to the height of those bars
disable it on Android (unless you have disabled keyboard/soft input handling in your manifest).

React Native ImageBackground showing white image only

My ImageBackground module is only showing a white background. The code shown is basically a boilerplate template from React's official documentation on ImageBackgrounds. My uri is not null. "props.route.params.image" is type TakePictureResponse from react-native-camera module.
I look at similar questions to this one, the solutions to theirs were already implemented in mine.
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
image: {
flex: 1,
resizeMode: 'cover',
justifyContent: 'center',
}
})
render() {
return (
<View style={styles.container}>
<ImageBackground
source={{uri: this.props.route.params.image.uri}}
style={styles.image}
/>
</View>
);
}
You should mention height and width for image Styling and wrap the content inside ImageBackground (just use ImageBackground like a View Component)
image: {
resizeMode: 'cover',
justifyContent: 'center',
width:'100%',
height:'100%'
}
// Example
// App.js
import React from 'react';
import { StyleSheet, Text, View, ImageBackground} from 'react-native';
export default function App() {
const imageUrl = 'https://images.pexels.com/photos/312418/pexels-photo-312418.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'
return (
<View style={styles.container}>
<ImageBackground
source={{uri: imageUrl}}
style={styles.image}
>
<Text
style={{userSelect:'text',color: '#fff', fontWeight:'bold', fontSize:32, marginTop: 180}}>{'My Text Element'}</Text>
</ImageBackground>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
image: {
resizeMode: 'cover',
justifyContent:'flex-start',
alignItems:'center',
width:'100%',
height:'100%'
}
})
output
make sure { backgroundColor: undefined } for the View components inside the ImageBackground
if {backgroundColor: '#fff'} then the white layer will cover the background image
<ImageBackground
source={{uri: imageUrl}}
style={styles.image}
>
<View style={{
// (eliminate 'white' layers)
backgroundColor: undefined,
width:'100%', height:'100%', alignItems:'center'}}>
<Text style={{ color: '#fff',fontWeight:'bold',
fontSize:32, marginTop: 180}}> {'My Text Element'}
</Text>
</View>
</ImageBackground>

FlatList onRefresh doesn't work with SafeAreaView

Pull to refresh causes endless spinner and don't calling onRefresh when app tested on iPhone. On Android and iOS devices with home button everything works as expected.
ReactNative version: 0.58.3
When flex:1 removed from container style, everything works fine but it ruins a markdown of screen. Using ScrollView causes same problem.
render() {
return (
<View style={styles.container}>
<SafeAreaView style={styles.safeAreaView}>
<Toolbar
leftElement="menu"
centerElement="sometext"
style={{ container: { backgroundColor: '#ffa500' } }}
searchable={{
autoFocus: true,
placeholder: 'Search',
onChangeText: text => this.searchFilterFunction(text),
onSearchCloseRequested: () => this.resetSearchFilter()
}}
onLeftElementPress={() => this.props.navigation.openDrawer()}
/>
</SafeAreaView>
<FlatList
data={this.state.data}
keyExtractor={this.keyExtractor}
ItemSeparatorComponent={this.renderSeparator}
contentContainerStyle={{paddingLeft: '3%', paddingBottom: '4%'}}
refreshing={this.state.refreshing}
onRefresh={this.getData}
renderItem={({item}) =>
<PartnerCardComponent
partnerName={item.name}
discount={item.discount}
url={item.url}
image={item.image}
phone={item.telephones}
address={item.locations}
description={item.description}
navigation={this.props.navigation}
/>
}
/>
<SafeAreaView style={styles.bottomArea}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white'
},
safeAreaView: {
backgroundColor: '#ffa500',
shadowColor: 'gray',
shadowOffset: {height: 1, width: 0},
shadowOpacity: 0.5,
},
bottomArea: {
backgroundColor: 'white',
shadowColor: 'white',
shadowOffset: {height: -5, width: 0},
shadowOpacity: 5,
}
});
Expected: updating FlatList data
Receiving: endless spinner rotation, onRefresh doesn't calling.
I had a similar situation (though my FlatList was inside a SafeAreaView, not surrounded by them). What worked for me was using a, in my opinion, vaguely described RefreshControl component rather than setting the onRefresh and refreshing props directly. Without seeing the rest of your code (and importing RefreshControl from react-native) I've just dropped it in:
...
<FlatList
data={this.state.data}
keyExtractor={this.keyExtractor}
ItemSeparatorComponent={this.renderSeparator}
contentContainerStyle={{paddingLeft: '3%', paddingBottom: '4%'}}
refreshControl={<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this.getData}
/>}
renderItem={({item}) =>
<PartnerCardComponent
partnerName={item.name}
discount={item.discount}
url={item.url}
image={item.image}
phone={item.telephones}
address={item.locations}
description={item.description}
navigation={this.props.navigation}
/>
}
/>
...

React Native Image URI Not Showing

While rendering a set of row components in ListView, my uri images seem not to be showing up at all. I have included Image in the react module. My custom component is the following:
var url = rowData.mediaGroups[0].contents[0].url;
if (url.indexOf('w=150') > -1)
{
url.replace("w=150", "w=500");
}
return <ArticlePreview
category={rowData.categories[0]}
key={sectionID}
heartText={'2.9k'}
categoryPress={this.onCategoryDetailsPress}
selected={false}
source={{ uri: url }}
text={rowData.title.toLowerCase().replace(' ','')}
onPress={this.onArticleDetailsPress} />
The component has multiple properties and is used throughout my app (and works there with link images). But for some reason the component just stopped working in the app for uri images. Not sure what to do. Here is the code for the component itself:
//component for article preview touchable image
/* will require the following
- rss feed and api
- user's keyword interests from parse In home.js
- parse db needs to be augmented to include what they heart
- parse db needs to be augmented to include what they press on (like google news)
*/
var React = require('react-native');
var {
View,
StyleSheet,
Text,
Image,
TouchableHighlight,
} = React;
//dimensions
var Dimensions = require('Dimensions');
var window = Dimensions.get('window');
var ImageButton = require('../../common/imageButton');
var KeywordBox = require('../../authentication/onboarding/keyword-box');
//additional libraries
module.exports = React.createClass({
//onPress function that triggers when button pressed
//this.props.text is property that can be dynamically filled within button
/* following props:
- source={this.props.source}
- onPress={this.props.onPress}
- {this.props.text}
- {this.props.heartText}
- key={this.props.key}
- text={this.props.category}
- this.props.selected
*/
render: function() {
return (
<TouchableHighlight
underlayColor={'transparent'}
onPress={this.props.onPress} >
<Image source={this.props.source} style={[styles.articlePreview, this.border('red')]}>
<View style={[styles.container, this.border('organge')]}>
<View style={[styles.header, this.border('blue')]}>
<Text style={[styles.previewText]}>{this.props.text}</Text>
</View>
<View style={[styles.footer, this.border('white')]}>
<View style={[styles.heartRow, this.border('black')]}>
<ImageButton
style={[styles.heartBtn, , this.border('red')]}
resizeMode={'contain'}
onPress={this.onHeartPress}
source={require('../../img/heart_btn.png')} />
<Text style={[styles.heartText]}>{this.props.heartText} favorites</Text>
</View>
<KeywordBox
style={[styles.category, this.border('blue')]}
key={this.props.key}
text={this.props.category}
onPress={this.props.categoryPress}
selected={this.props.selected} />
</View>
</View>
</Image>
</TouchableHighlight>
);
},
onHeartPress: function() {
//will move this function to a general module
},
border: function(color) {
return {
//borderColor: color,
//borderWidth: 4,
}
},
});
var styles = StyleSheet.create({
heartText: {
color: 'white',
fontSize: 12,
fontWeight: 'bold',
alignSelf: 'center',
marginLeft: 5,
},
heartRow: {
flexDirection: 'row',
justifyContent: 'space-around',
alignSelf: 'center',
justifyContent: 'center',
},
heartBtn: {
height: (92/97)*(window.width/13),
width: window.width/13,
alignSelf:'center',
},
category: {
fontFamily: 'Bebas Neue',
fontSize: 10,
fontWeight: 'bold'
},
header: {
flex: 3,
alignItems: 'center',
justifyContent: 'space-around',
marginTop: window.height/30,
},
footer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
margin: window.height/50,
},
container: {
flex: 1,
backgroundColor: 'black',
opacity: 0.6,
},
articlePreview: {
flex: 1,
height: window.height/3.2,
width: window.width,
flexDirection: 'column'
},
previewText: {
fontFamily: 'Bebas Neue',
fontSize: 23,
color: 'white',
alignSelf: 'center',
textAlign: 'center',
margin: 5,
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0
},
});
The UI currently looks like this with not images (has black background):
Checked my rss feeds - some were unreliable resulting in messing with the way the images were being shown

Resources