I have a Touchablehighlight that I need to position absolute, but it becomes unclickable after I do it.
What could cause this? It functions like it should if I dont have the position set to absolute.
I have a Touchablehighlight that I need to position absolute, but it becomes unclickable after I do it.
What could cause this? It functions like it should if I dont have the position set to absolute.
Solution was to change the order of the components.
What i originally had:
<TouchableHighLight><Text>Click me</Text></TouchableHighlight> <View> .... </View> This was the fix:
<View>...</View> <TouchableHighLight><Text>Click me</Text></TouchableHighlight> absolute, the second element renders above it. When we change the order, the absolute element (Which is now the second element) loads on top.zIndex of the Component you want to be on top of everything. Even a zIndex: 1 will do it in most cases...Dude just go and add zIndex : 1 to the view containing the buttons and boom you are done in most of the cases. Also note adding elevation adds shadow to android button and sometimes elevation may also be a issue if its added to parent and not added to child then the child button may not work.(Rare Case)
eg:
buttonContainers: { zIndex: 1, alignSelf: 'flex-end', position: 'absolute', top:5, right: 5, height: 40, borderWidth: 1, justifyContent: 'center', alignContent: 'center', width: 80 }, react-native-vector-icons. zIndex: 1 was a critical bonus attribute. Otherwise having an <Icon /> component inside a <TouchableOpacity /> causes a problem where the icon doesn't act as a hitspace but the area surrounding the icon did, so it made the button 100% garbage sauce.SOLVED:
I faced this issue today. I have solved it.
Import TouchableOpacity from react-native-gesture-handler instead of react-native.
Before:
import {TouchableOpacity} from "react-native"; After:
import {TouchableOpacity} from 'react-native-gesture-handler' TouchableOpacity from react-native-gesture-handler and changed it to import from react-native. Yeah it is other way around, however it gave a spark and worked :pI used TouchableOpacity inside an absolute view. The onPress function was not called after press it. But the opacity changed. I've tried all the above solutions, but none works.
My solutions is use onPressIn instead of onPress.
It seems like the inner action of Touchable* is weird in ReactNative when it's in an absolute view.
After trying everything for two hours, the solution I found was to change my button position.
Before ...
export default class Navbar extends Component { componentDidMount() { console.log(this.props); } render() { return ( <View style={styles.content}> <TouchableOpacity onPress={this.props.openModal} style={styles.containerButton} > <Text>New</Text> </TouchableOpacity> <Text style={styles.textCenter}>Remember me</Text> </View> ); } } const styles = StyleSheet.create({ content: { paddingTop: 30, paddingBottom: 10, backgroundColor: '#81C04D', flexDirection: 'row' }, containerButton: { position: 'absolute', top: 30, left: 8 }, textCenter: { flex: 1, textAlign: 'center', fontWeight: 'bold' } }); After ...
export default class Navbar extends Component { componentDidMount() { console.log(this.props); } render() { return ( <View style={styles.content}> <Text style={styles.textCenter}>Remember me</Text> <TouchableOpacity onPress={this.props.openModal} style={styles.containerButton} > <Text>New</Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ content: { paddingTop: 30, paddingBottom: 10, backgroundColor: '#81C04D', flexDirection: 'row' }, containerButton: { position: 'absolute', top: 30, left: 8 }, textCenter: { flex: 1, textAlign: 'center', fontWeight: 'bold' } }); It works!!!
use zIndex: 1 in view, it'll work.
<View style={{position : 'absolute', marginTop : 25, zIndex: 1}}>
More details can be found here :
One more solution.....
For me what worked was a combination of things....
import { TouchableOpacity } from 'react-native-gesture-handler' and I WRAPPED my TouchableOpacity in a View.
before:
<TouchableOpacity onPress={()=> addCallback()} style={styles.addButtonHolder} > <PlusCircle style={styles.addButton} width={70} height={70} stroke={"white"} strokeWidth={3}/> </TouchableOpacity> after:
<View style={styles.addButtonHolder}> <TouchableOpacity onPress={()=> addCallback()}> <PlusCircle style={styles.addButton} width={70} height={70} stroke={"white"} strokeWidth={3}/> </TouchableOpacity> </View> StyleSheet:
const styles = StyleSheet.create({ addButtonHolder: { position: 'absolute', bottom: 70, right: 10, justifyContent: 'center', alignItems: 'center', zIndex: 1, }, addButton: { backgroundColor: '#b4cffa', borderRadius: 35 } }) This props help to disable ScrollView to catch all touches and let child handles
keyboardShouldPersistTaps='always' https://reactnative.dev/docs/scrollview#keyboardshouldpersisttaps
I ran into a similar problem, what I did was, I enclosed the whole thing into a new View and instead of giving 'absolute' position to the TouchableOpacity, I gave it to the parent View. That made the Opacity again clickable somehow. Here is the code snippet of before and after
My Code before
<TouchableOpacity onPress={() => { console.log("hah"); }} style={{ height: 50, width: 50, backgroundColor: 'rgb(90,135,235)', borderRadius: 25, alignItems: 'center', justifyContent: 'center', right: 0,position:'absolute' }}> <Image source={require('../assets/images/element-acorn-white.webp')} style={{ height: 30, width: 30, resizeMode: 'contain' }} /> </TouchableOpacity> After Wrapping into a View with 'absolute'
<View style={{ alignItems: 'flex-end', position: 'absolute', bottom: Dimensions.get('screen').height / 5 }}> <TouchableOpacity onPress={() => { console.log("hah"); }} style={{ height: 50, width: 50, backgroundColor: 'rgb(90,135,235)', borderRadius: 25, alignItems: 'center', justifyContent: 'center', right: 0, }}> <Image source={require('../assets/images/element-acorn-white.webp')} style={{ height: 30, width: 30, resizeMode: 'contain' }} /> </TouchableOpacity> </View> He guy, I took a long time to find out why this happens. I tested a lot of the solution here. Two things worked for:
elevation and zIndex on the wrapper container<View style={{ zIndex:0 }> ... <View style={{ position: 'absulote', zIndex:10 ,elevation: 10 }}> <TouchableHighLight><Text>Click me</Text></TouchableHighlight> </View> <View> .... </View> ... </View> If I am right, the reason for that is even that the button is shown, Android treats differently the layers of the Press events and you need to set a low level for the rest of your components. Defining a lower level for your wrapper component, all its children without this attribute will automatically inherit from the parent.
My problem was quite different, a backgroundColor style property was set on the container of my button. The color didn't work. I missed to remove this useless property. Finally, this backgroundColor was making an invisible sheet above my button. Removing it make it clickable again.
I faced the problem only on Android.