Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the direction of the animation in StackNavigator?

How to change the direction of the animation in StackNavigator?

Current Behavior

When user goes to another screen, the screen flies from bottom to top.

Expected Behavior

When user goes to another screen, the screen flies from right to left. (Like Facebook or Instagram!)

StackNavigator Code

export default StackNavigator ({     Main: {         screen: MainScreen,     },     ... }, {     navigationOptions: ({navigation, screenProps}) => ({         tabBarOnPress: blahblaj     }),     lazy: true     // I guess we can do something here }); 
  • If we can set the animation time, it will be even better! Currently it looks like the screen flies from middle of the screen to top. I want natural animation like Facebook or Instagram :)

Thanks in advance,

like image 976
merry-go-round Avatar asked Dec 29 '17 06:12

merry-go-round


People also ask

How do you navigate in stack Navigator?

navigate ​ If the screen is not present, it'll push a new screen. For example, if you have a stack with the history Home > Profile > Settings and you call navigate(Profile) , the resulting screens will be Home > Profile as it goes back to Profile and removes the Settings screen.

What is stack screen?

Stack Navigator provides a way for your app to transition between screens where each new screen is placed on top of a stack. By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, use OS default animation on Android.


2 Answers

For react navigation > 5.0:

import {   CardStyleInterpolators,   createStackNavigator, } from '@react-navigation/stack';      const Stack = createStackNavigator(); export default () => (   <Stack.Navigator     screenOptions={{       cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS     }}   >     <Stack.Screen name="Screen 1" component={ScreenComponent1} />     <Stack.Screen name="Screen 2" component={ScreenComponent2} />   </Stack.Navigator> ); 

You may also want to use headerStyleInterpolator: HeaderStyleInterpolators.forUIKit

More info here: https://reactnavigation.org/docs/stack-navigator/#pre-made-configs

like image 111
Daniel Loiterton Avatar answered Sep 22 '22 05:09

Daniel Loiterton


For react navigation < 5.0

On iOS it's standard behavior. Android requires a little bit of configuration. There are two options you can use to set screen transitions: mode and transitionConfig. In this case transitionConfig will work:

import CardStackStyleInterpolator from 'react-navigation/src/views/CardStack/CardStackStyleInterpolator'; // this path can be different depending on react-navigation version, this one is for @1.0.0-beta.15   export default StackNavigator ({     Main: {         screen: MainScreen,     },         ... }, {    transitionConfig: () => ({         screenInterpolator: CardStackStyleInterpolator.forHorizontal,    }), }) 

We use CardStackStyleInterpolator from react-navigation source, but you can provide custom transition if you want, here is how to make one or here or this article.

mode is more for default behavior:

export default StackNavigator ({     Main: {         screen: MainScreen,     },     ... }, {     mode: 'card',     navigationOptions: ({navigation, screenProps}) => ({         tabBarOnPress: blahblaj     }),     lazy: true }); 

mode can have only two values:

  • card - Use the standard iOS (right to left) and Android (bottom to top) screen transitions. This is the default.

  • modal - Make the screens slide in from the bottom which is a common iOS pattern. Only works on iOS, has no effect on Android.

For react navigation >= 5.0:

import {   CardStyleInterpolators,   createStackNavigator, } from '@react-navigation/stack';      const Stack = createStackNavigator(); export default () => (   <Stack.Navigator     screenOptions={{       cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS     }}   >     <Stack.Screen name="Screen 1" component={ScreenComponent1} />     <Stack.Screen name="Screen 2" component={ScreenComponent2} />   </Stack.Navigator> ); 

You may also want to use headerStyleInterpolator: HeaderStyleInterpolators.forUIKit

More info here: https://reactnavigation.org/docs/stack-navigator/#pre-made-configs

like image 20
zarcode Avatar answered Sep 24 '22 05:09

zarcode