react-navigation:禁用模式动画

时间:2018-12-03 03:54:55

标签: react-native react-navigation

是否可以禁用React Navigation的模式动画?

class HomeScreen extends React.Component {
  static navigationOptions = ({ navigation }) => {
    const params = navigation.state.params || {};

    return {
      headerLeft: (
        <Button
          onPress={() => navigation.navigate('MyModal')}
          title="Info"
          color="#fff"
        />
      ),
      /* the rest of this config is unchanged */
    };
  };

  /* render function, etc */
}

class ModalScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text style={{ fontSize: 30 }}>This is a modal!</Text>
        <Button
          onPress={() => this.props.navigation.goBack()}
          title="Dismiss"
        />
      </View>
    );
  }
}

const MainStack = createStackNavigator(
  {
    Home: {
      screen: HomeScreen,
    },
    Details: {
      screen: DetailsScreen,
    },
  },
  {
    /* Same configuration as before */
  }
);

const RootStack = createStackNavigator(
  {
    Main: {
      screen: MainStack,
    },
    MyModal: {
      screen: ModalScreen,
    },
  },
  {
    mode: 'modal',
    headerMode: 'none',
  }
);

示例可从React Native官方文档中获取:https://reactnavigation.org/docs/en/modal.html

2 个答案:

答案 0 :(得分:1)

解决方案

使用transitionSpec中的transitionConfig进行自定义屏幕转换。并将过渡持续时间设为零。 Official

示例(未经测试)

const ModalNavigator = createStackNavigator(
  {
    Main: { screen: Main },
    Login: { screen: Login },
  },
  {
    headerMode: 'none',
    mode: 'modal',
    defaultNavigationOptions: {
      gesturesEnabled: false,
    },
    transitionConfig: () => ({
      transitionSpec: {
        duration: 0,
      },
    })
...

答案 1 :(得分:0)

这不是您想要的,但是您可以使用内置的Modal组件来呈现屏幕。这样做的好处是Modal组件具有一个animationtype prop,您可以将其设置为“ none”以获取无动画的模态。