Swipe between screens isn't working in react-native, react navigation - react-native

I am trying to get my app to swipe between screens using react navigation. I have tried setting swipeEnabled, animationEnabled and gesturesEnabled to true but nothing has worked so far.
I am new to react navigation and thought i would give it a try.
I am using createStackNavigator so i dont know if i need to change that.
import React, { Component } from 'react';
import { createBottomTabNavigator,
createStackNavigator, createAppContainer } from 'react-navigation';
const Worcester = createStackNavigator(
{
Wrh,
},
{
initalRouteName: Wrh,
defaultNavigationOptions: {
title: 'Worcester',
headerLeft: <ActionBarImage />,
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#000000'
},
tabBarOptions: {
labelStyle: {
fontSize: 40
}
},
headerStyle: {
backgroundColor: '#FFFAFA',
borderBottomColor: '#ffffff',
borderBottomWidth: 3,
},
HeaderTitle: 'Test',
backgroundColor: '#FFDEAD',
swipeEnabled: true,
animationEnabled: true,
gesturesEnabled: true
}
});
const Alex = createStackNavigator(
{
Alx,
},
{
initalRouteName: Alx,
defaultNavigationOptions: {
title: 'Redditch',
headerLeft: <ActionBarImage />,
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#000000'
},
tabBarOptions: {
labelStyle: {
fontSize: 20
}
},
headerStyle: {
backgroundColor: '#FFFAFA',
borderBottomColor: '#ffffff',
borderBottomWidth: 3,
swipeEnabled: true,
animationEnabled: true,
gesturesEnabled: true
},
},
});
const TabNavigator = createBottomTabNavigator(
{
Worcester: { screen: Worcester },
Redditch: { screen: Alex },
},
{
tabBarOptions: {
activeTintColor: 'blue',
inactiveTintColor: '#605F60',
inactiveBackgroundColor: 'grey',
activeBackgroundColor: '#FFFAFA',
labelStyle: {
fontSize: 20,
marginTop: 0,
paddingTop: 0
},
style: {
paddingTop: 10
},
swipeEnabled: true,
animationEnabled: true,
gesturesEnabled: true
},
}
);
export default createAppContainer(TabNavigator);

You should use a topTabNavigator/bottomTabNavigator to swipe between screens on the same stack.
Then you use your stacknavigators,screens like that :
...
import { createMaterialTopTabNavigator, createStackNavigator } from 'react-navigation';
...
const someTabNavigator= createMaterialTopTabNavigator(
{
SomeScreen: {
screen:TheScreen,
navigationOptions: {
tabBarAccessibilityLabel: 'The Screen',
tabBarLabel: ({ tintColor }) => <LabelTitle tintColor={tintColor} label="The Screen" />,
},
},
SomeOtherScreen: {
screen: TheOtherScreen,
navigationOptions: {
tabBarAccessibilityLabel: 'The Other Screen',
tabBarLabel: ({ tintColor }) => <LabelTitle tintColor={tintColor} label="The Other Screen" />,
},
},
},
{
// Configs and etc.
swipeEnabled: true,
lazy: true,
optimizationsEnabled: true,
animationEnabled: true,
tabBarPosition: 'top',
tabBarOptions: {
scrollEnabled: true,
style: {
backgroundColor: colors.white,
},
inactiveTintColor: inactiveTintLabelColor,
activeTintColor: activeTintLabelColor,
indicatorStyle: {
backgroundColor: colors.primaryColor,
},
tabStyle: {
width: screen.screenWidth >= 600 ? 210 : 120,
}
},
}
)

Related

How to add a drawer navigator React-Navigation v4

I have this code for creating a drawer navigator. But I can't open my drawer through left to right slide. Can anyone please help me to solve my problem. I am new to react-native. I am using react-navigation v4 and react-navigation-drawer for the drawer navigator.
const EMBotomTabScreen = createDrawerNavigator ({
SUMMARY: {
screen : EMNavigator,
navigationOptions :{
animationEnabled : 'true',
// headerLeft : <HamburgerIcon navigationProps={ navigation }/>,
swipeEnabled: true,
tabBarOptions: {
activeTintColor: '#369841',
//activeBackgroundColor : '#f2f2f2',
inactiveTintColor : 'gray',
inactiveBackgroundColor : '#adc1b8',
borderRightWidth:2,
borderRightColor:'blue',
labelStyle: {
fontSize: 13,
},
style: {
backgroundColor: '#f2f2f2',
// borderRightWidth:2,
// borderRightColor:'blue'
},
indicatorStyle: {
backgroundColor: 'red',
},
},
tabStyle: {
backgroundColor: 'green',
borderRightWidth:2,
borderRightColor:'blue'
},
indicatorStyle : {
backgroundColor : 'red'
},
tabBarIcon: ({ focused, tintColor }) => {
const iconName = `ios-desktop`;
return <Image source={require('../assets/aaaaa.png')} size={27} color={tintColor} />;
},
}
},
DEMAND: {
screen : EMDemandNavigator,
navigationOptions : {
tabBarOptions: {
activeTintColor: '#369841',
// activeBackgroundColor : '#f2f2f2',
inactiveTintColor : 'gray',
inactiveBackgroundColor : '#adc1b8',
labelStyle: {
fontSize: 13,
},
style: {
backgroundColor: '#f2f2f2',
},
},
tabBarIcon: ({ focused, tintColor }) => {
const iconName = `ios-cloud`;
return <Image source={require('../assets/aaaaaaa.png')} size={27} color={tintColor} />;
},
}
},
ALARM: {
screen : EMAlarmNavigator,
navigationOptions : {
animationEnabled : 'true',
swipeEnabled: true,
tabBarOptions: {
activeTintColor: '#369841',
//activeBackgroundColor : '#f2f2f2',
inactiveTintColor : 'gray',
inactiveBackgroundColor : '#adc1b8',
borderRightWidth:2,
borderRightColor:'blue',
labelStyle: {
fontSize: 13,
},
style: {
backgroundColor: '#f2f2f2',
// borderRightWidth:2,
// borderRightColor:'blue'
},
indicatorStyle: {
backgroundColor: 'red',
},
},
tabStyle: {
backgroundColor: 'green',
borderRightWidth:2,
borderRightColor:'blue'
},
indicatorStyle : {
backgroundColor : 'red'
},
tabBarIcon: ({ focused, tintColor }) => {
const iconName = `bell-o`;
return <Icons name={iconName} size={27} color={tintColor} />;
},
}
}
You can get an idea from the below given code.
import Splash from '../Splash/Splash';
import Home from '../screens/Home';
import DrawerScreen from '../screens/DrawerScreen'
//And many more
import {createStackNavigator,
createSwitchNavigator,
createAppContainer,
createDrawerNavigator} from 'react-navigation';
const AppStack = createDrawerNavigator({
Home:Home,
},{
contentComponent: DrawerScreen,
defaultNavigationOptions: {
drawerWidth: Dimensions.get('window').width - 120,
},
});
const AppNavigator = createStackNavigator({
Splash:Splash,
Login:Login,
SignUp:SignUp },,{
defaultNavigationOptions: {
header: null,
gesturesEnabled: false
},
initialRouteName: "Splash");
const AppContainer = createAppContainer(AppNavigator);
Whenever you want to open the drawer like on click of the drawer icon. then add these line in the onPress definition.
this.props.navigation.openDrawer();

Indicator is not rendering on iOS and Android

I am trying to render the topBar on tab navigation and it's not showing
import { createBottomTabNavigator, createAppContainer } from "react-navigation";
const TabNavigator = createBottomTabNavigator(
{
Dashboard: {
screen: Dashboard,
navigationOptions: {
title: "Dashboard",
tabBarIcon: ({ tintColor }) => (
<Icon
name="home"
type="Entypo"
style={{
color: tintColor,
}}
color={tintColor}
/>
),
},
},
},
{
tabBarPosition: "top",
tabBarOptions: {
activeTintColor: "#5D10F6",
inactiveTintColor: "gray",
showIcon: true,
tintColor: "#333",
animationEnabled: false,
showLabel: false,
scrollEnabled: true,
tabBarPosition: "top",
indicatorStyle: {
backgroundColor: "red",
},
},
}
);
Note: createTabNavigator is removed in react-navigation#3.x. Please use createBottomTabNavigator and/or createMaterialTopTabNavigator instead.
However createBottomTabNavigator no longer supports indicatorStyle :(

How can i import the defaultNavigationOptions property on a screen?

I'm trying to import the react-navigation property into my screens the problem is that I always import the same defaultNavigationOptions for the different Stacks then to optimize the code I want to create as a kind of function so I only import it once on each screen without having to write it many times as I did, then my code so they understand more.
const BloquesStack = createStackNavigator(
{
BLOQUES: {
screen: ScreenBloquesRedux,
navigationOptions: ({ navigation }) => {
return {
headerLeft: <ButtonMenu navigation={navigation} />,
headerRight: <ButtonHome navigation={navigation} />
};
}
},
DetalleBloques: {
screen: DetalleBloque
},
IntegrantesBloque: {
screen: IntegrantesBloque
}
},
{
defaultNavigationOptions: {
headerBackTitle: "Atras",
headerTitleStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 20,
textAlign: "center",
color: white,
flex: 1
},
headerStyle: { backgroundColor: blue, height: 60 },
headerTintColor: white
}
}
);
export { BloquesStack };
const ComisionesStack = createStackNavigator(
{
COMISIONES: {
screen: ComisionesRedux,
navigationOptions: ({ navigation }) => {
return {
headerLeft: <ButtonMenu navigation={navigation} />,
headerRight: <ButtonHome navigation={navigation} />
};
}
}
},
{
defaultNavigationOptions: {
headerBackTitle: "Atras",
headerTitleStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 20,
textAlign: "center",
color: white,
flex: 1
},
headerStyle: { backgroundColor: blue, height: 60 },
headerTintColor: white
}
}
export { ComisionesStack };
const DrawerNavigator = createDrawerNavigator(
{
Diputados: { screen: DiputadosStack },
Bloques: { screen: BloquesStack },
Interbloques: { screen: InterBloquesStack },
Comisiones: { screen: ComisionesStack },
Autoridades: { screen: AutoridadesStack },
"Sesion En Vivo": { screen: SesionEnVivoStack },
"Diputados TV": { screen: DiputadosTVStack },
"Reglamentos HCDN": { screen: PDFReglamentosStack }
},
{
contentComponent: CustomDrawerContentComponent,
drawerWidth: width / 2,
contentOptions: {
activeTintColor: white,
activeBackgroundColor: Gris_Drawer,
inactiveTintColor: "rgb(105,105,104)",
itemsContainerStyle: {
textAlign: "center"
},
labelStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 17,
marginTop: 8,
marginLeft: 10
}
},
iconContainerStyle: {
opacity: 1
}
}
);
I just want to import default Navigation Options I do not intend to modify my navigation, I just want to know if it can be done. Thank you very much already
Create a separate object with the defaultNavigationOptions
const defaultNavigationOptions = {
headerBackTitle: "Atras",
headerTitleStyle: {
fontFamily: "RobotoCondensed-Regular",
fontWeight: "100",
fontSize: 20,
textAlign: "center",
color: white,
flex: 1
},
headerStyle: { backgroundColor: blue, height: 60 },
headerTintColor: white
}
const BloquesStack = createStackNavigator(
{ /* routes */ },
{ defaultNavigationOptions }
)
const ComisionesStack = createStackNavigator(
{ /* routes */ },
{ defaultNavigationOptions }
)

I couldn't use react-navigation tab bar. How to use this?

Now I am trying to create React Native app on Expo and use React-Navigation Tab Bar but I could't.
Actually I don't get any error but this code below doesn't work.
No warning as well.
import { createBottomTabNavigator, createAppContainer } from 'react-
navigation';
import Icon from 'react-native-vector-icons/FontAwesome';
import Home from './src/Screens/Home';
import Help from './src/Screens/Help';
const App = createBottomTabNavigator(
{
Home: {
screen: Home,
defaultNavigationOptions: {
tabBarIcon: ({ tintColor }) => {
<Icon name="home" style={{ width: 25, height: 25, tintColor:
tintColor}}/>
},
title: 'Home',
},
},
Help: { screen: Help },
},
{
swipeEnabled: false, //Android用
tabBarOptions: {
activeTintColor: '#DE628D',
inactiveTintColor: '#707070',
},
},
);
export default createAppContainer(App);
the tab works fine, but if you meant, there's no icon, try this instead
navigationOptions: {
tabBarIcon: ({ tintColor, activeTintColor }) => (
<Icon name="home" size={24} color={tintColor} />)
},
Please try to implement this way. This is copy of my tabNavigator. Hope this will be helpful for you.
const TabRouter = createBottomTabNavigator(
{
HomeAfterLoginScreen: { screen: A },
ShowListAlertScreen: { screen: B },
ShowListProfessionScreen: { screen: C },
MyAccountScreen: { screen: F }
},
{
tabBarPosition: "bottom",
tabBarOptions: {
style: { backgroundColor: "#50bcb8" },
showIcon: true,
showLabel: true,
gesturesEnabled: true,
indicatorStyle: { borderBottomWidth: 3, borderBottomColor: Style.color },
inactiveTintColor: "#fff",
activeTintColor: "#fff",
tabStyle: { justifyContent: "center", alignItems: "center" }
}
});
I got it. I have solved this issue.
const App = createBottomTabNavigator(
{
Favorite: {
screen: FavoriteShops,
navigationOptions: {
tabBarLabel: 'お気に入り',
tabBarIcon: ({ tintColor }) => (
<Icon name="heart" size={25} color={tintColor} />
),
},
},
Home: {
screen: Home,
navigationOptions: {
tabBarLabel: 'ホーム',
tabBarIcon: ({ tintColor }) => (
<Icon name="home" size={30} color={tintColor} />
),
},
},
Help: {
screen: Help,
navigationOptions: {
tabBarLabel: 'その他',
tabBarIcon: ({ tintColor }) => (
<Icon name="bars" size={25} color={tintColor} />
),
},
},
},
{
swipeEnabled: false, //Android用
tabBarOptions: {
showIcon: true,
showLabel: true,
activeTintColor: '#DE628D',
inactiveTintColor: '#707070',
style: {
width: '100%',
height: 70,
},
tabStyle: {
paddingTop: 20,
},
},
},
);

Header is empty after react-navigation updated to v2

After updating react-navigation package of my react-native project from 1.5.8 to 2.0.1 my header became empty on all tabs of TabNavigator. On other screens it works as before.
Example of tab Dashboard:
class Dashboard extends PureComponent {
static navigationOptions = ({ navigation }) => {
return {
title: strings.dashboard_header_title,
headerLeft: renderMenu(navigation)
};
};
...
}
export default connect((state, ownProps) => {
...
})(Dashboard);
const renderMenu = navigation => {
return <ImageButton
style={styles.padding}
imageStyle={styles.tintWhite}
image={require('../../../res/images/menu.png')}
onPress={navigation.state.params && navigation.state.params.toggleSideMenu}/>
}
It worked with old version of react-navigation. I use react-redux and tab navigator inside stack navigator.
TabNavigator:
const routeConfig = {
Dashboard: {
screen: Dashboard,
resource: 'dashboard',
navigationOptions: { tabBarIcon: ({tintColor}) => <Image style={{tintColor}} source={require('../../../res/images/tab-dashboard.png')}/> }
},
...
};
const drawConfig = {
lazy: false,
animationEnabled: false,
swipeEnabled: false,
tabBarPosition: 'bottom',
tabBarOptions: {
upperCaseLabel: false,
activeBackgroundColor: 'white',
activeTintColor: colors.main,
inactiveTintColor: colors.normal,
showIcon: true,
style: {
height: 48,
backgroundColor: 'white',
borderTopColor: colors.border,
borderTopWidth: values.borderWidth,
elevation: 0
},
labelStyle: {
fontSize: 11,
marginBottom: 0
},
tabStyle: {
padding: 3,
paddingTop: Platform.OS === 'android' ? 4 : 3
},
indicatorStyle: {
height: 0,
width: 0
}
}
}
const InnerTabNavigator = createBottomTabNavigator(routeConfig, drawConfig);
class TabBarNavigator extends PureComponent
{
...
render() {
return (
<InnerTabNavigator
{...this.props.ownProps}
navigation={{
...this.props.navigation,
state: this.state
}}
/>
);
}
...
}
TabBarNavigator.router = InnerTabNavigator.router;
MainNavigator:
const MainNavigatorInner = createStackNavigator({
...
TabBarNavigator: { screen: TabBarNavigator },
...
}, {
initialRouteName: 'SignIn',
headerMode: 'screen',
navigationOptions: ({ navigation }) => {
return {
headerTintColor: 'white',
headerTitleStyle: styles.headerTitle,
headerStyle: {
backgroundColor: colors.main,
shadowColor: 'transparent',
elevation: 0,
borderBottomWidth: values.borderWidth,
borderBottomColor: colors.borderHeader,
height: values.headerHeight
},
};
},
cardStyle: {
backgroundColor: colors.background
}
});
export default class MainNavigator extends PureComponent {
...
render() {
return (
...
<MainNavigatorInner
ref='navigation'
screenProps={this.screenProps}
onNavigationStateChange={this._onNavigationStateChange}/>
...
);
}
...
}
I had the same issue yesterday when I updated my react-navigation too. I know that this is not optimal but try adding the tabBarIcon in your drawconfig:
const drawConfig = {
lazy: false,
animationEnabled: false,
swipeEnabled: false,
tabBarPosition: 'bottom',
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
const { routeName } = navigation.state;
let icon;
if (routeName === "SomeRouteName") {
icon = require("src/assets/someFile");
} else if (routeName === "Some other route name") {
if (focused) {
icon = require("src/assets/someFile.png");
} else {
icon = require("src/assets/someFile.png");
}
}
return <Image source={icon} style={{ width: 30, height: 30 }} />;
}
}),
tabBarOptions: {
upperCaseLabel: false,
activeBackgroundColor: 'white',
activeTintColor: colors.main,
inactiveTintColor: colors.normal,
showIcon: true,
style: {
height: 48,
backgroundColor: 'white',
borderTopColor: colors.border,
borderTopWidth: values.borderWidth,
elevation: 0
},
labelStyle: {
fontSize: 11,
marginBottom: 0
},
tabStyle: {
padding: 3,
paddingTop: Platform.OS === 'android' ? 4 : 3
},
indicatorStyle: {
height: 0,
width: 0
}
}
}
I had same ıssue too my solution is added my stacknavigator
headerMode: 'auto' property, I using expo template and RootNavigation file is added headerMode: 'auto' too, it worked for me,
Roote navigation like this =>
const RootStackNavigator = createStackNavigator(
{
Main: {
screen: MainTabNavigator,
},
},
{
navigationOptions: () => ({
headerTitleStyle: {
fontWeight: 'normal',
},
}),
headerMode: 'auto'
}
);