I added custom topBar to screen and added custom button inside the topBar, using TouchableOpacity, TouchableNativeFeedback but when I click on button nothing happens, onPress even alert not working
import React, { Component } from "react";
import { View, Text, TouchableOpacity, TouchableNativeFeedback, Platform } from "react-native";
import styles from './screenHeaderWithHamburgerStyles';
import Icon from "react-native-vector-icons/Ionicons";
class ScreenHeaderWithHamburger extends Component {
showSideDrawer = () => {
alert("showSideDrawer");
};
render(){
const hamburgerButton = (
<View style={styles.hamburgerButton}>
<Icon name="ios-menu" size={30} color="#fff" />
</View>
);
const hamburgerClickable = (Platform.OS === "android") ?(<TouchableNativeFeedback onPress={ () => this.showSideDrawer() }>
{hamburgerButton}
</TouchableNativeFeedback> ) : (<TouchableOpacity onPress={ () => this.showSideDrawer() }>{hamburgerButton}</TouchableOpacity>);
return (
<View style={styles.headerBackground}>
{ hamburgerClickable }
</View>
)
}
}
export default ScreenHeaderWithHamburger;
export const signUpScreenOptions = {
layout: {
backgroundColor: '#ffffff',
orientation: ['portrait', 'landscape']
},
topBar: {
visible: true,
animate: false,
hideOnScroll: true,
buttonColor: '#f8a11b',
drawBehind: false,
shadowColor: 'red',
testID: 'topBar',
elevation: 0,
backButton: {
visible: false,
color: '#f8a11b'
},
background: {
component: {
name: 'dsprov1.HeaderCustomBackButton'
}
}
}
}
after export I import this file into my SignUpScreen and then using static method like following:
import { signUpScreenOptions } from './signUpScreenOptions';
...
static options(passProps) {
return signUpScreenOptions;
};
I've been trying to do the exact same thing... I think it simply renders the custom component as a background, and cannot be interacted with. I finally decided to hide the RNN TopBar entirely and render my own component instead. The tradeoff here would be that you'll have to add your custom topbar to every screen. For it to work on Android, along with visible: false
, you'll also have to set drawBehind: true
as well (#3224)
perhaps use title.component
instead. Background component is problematic for this use case.
Sorry but Still not works, when I use the component inside 'title
' the buttons are clickable but cant style the title field properly, whereas in 'background
' the styling works nice but buttons are not clickable
I am facing the same issue, Is there any method to make it clickable. I cannot add a custom Top bar because there are so many screens and my app is almost completed. I am just facing this problem for ios 10 or below otherwise it works on other devices. Please suggest, if anybody having a solution except applying a custom topBar. Thanks in advance.
Have the same problem. When I use a custom component for the topbar (image for a logo in my case) the buttons will not work. This persist in the whole stack, meaning that every pushed screen will have the same problem.
I think this issue is similar to https://github.com/wix/react-native-navigation/issues/3648#issuecomment-408680892 which also has the solution.
Using a custom component in title
, leftButtons
or rightButtons
with passProps
that has a callback such as onClick
.
Example:
Navigation.mergeOptions(this.props.componentId, {
topBar: {
title: {
component: {
name: 'navigation.NavBar',
aligment: 'center',
passProps: {
onClick: () => alert('Pressed button'),
}
},
},
}
});
Most helpful comment
I think this issue is similar to https://github.com/wix/react-native-navigation/issues/3648#issuecomment-408680892 which also has the solution.
Using a custom component in
title
,leftButtons
orrightButtons
withpassProps
that has a callback such asonClick
.Example: