简体   繁体   English

反应导航屏幕不会从纵向切换为横向

[英]React-navigation screens do not switch from portrait to landscape

Due to the deprecation of the Side Menu, I decided to switch my app to the new react-navigation as suggested by Expo. 由于侧边菜单的弃用,我决定按照Expo的建议将应用切换到新的反应导航。 Building an initial very basic prototype to emulate the current side menu, using a drawer navigator with one nested stack navigator and four plain screens, it appears that the app does not rotate from Portrait to Landscape mode. 使用带有一个嵌套堆栈导航器和四个普通屏幕的抽屉式导航器构建一个初始的非常基本的原型来模拟当前的侧菜单,看来该应用程序不会从纵向旋转到横向模式。 Needless to say, my current app, as developed without react-navigation, cleanly supports both portrait and landscape across all screens. 不用说,我当前的应用程序是在没有反应导航的情况下开发的,可以在所有屏幕上清晰地支持纵向和横向。

The documentation does not provide any relevant detail nor mentions any settings that would enable/disable screen rotation. 该文档没有提供任何相关的详细信息,也没有提及将启用/禁用屏幕旋转的任何设置。 I also was unable to find any reference elsewhere as to the expected behavior or explaining how to enable rotation. 我也找不到其他有关预期行为的参考,也无法解释如何启用旋转。

My package.json file is as follows: 我的package.json文件如下:

{
  "name": "pdpappnav",
  "version": "0.0.0",
  "description": "xxx",
  "author": "xxx",
  "private": true,
  "main": "node_modules/expo/AppEntry.js",
  "dependencies": {
    "connect": "^3.6.5",
    "expo": "^22.0.0",
    "react": "react@16.0.0-beta.5",
    "react-native": "https://github.com/expo/react-native/archive/sdk-22.0.1.tar.gz",
    "react-native-elements": "^0.17.0",
    "react-navigation": "^1.0.0-beta.15",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2"
  }
}

The main code portion of interest is in AppNavigation.js as follows 感兴趣的主要代码部分位于AppNavigation.js中,如下所示

import React from 'react';
import { Text, View } from 'react-native';
import { StackNavigator, DrawerNavigator, TabNavigator } from 'react-navigation';
import MainScreen from '../Containers/MainScreen';
import SideMenu from '../Components/SideMenu';
import AccountManagement from '../Containers/AccountManagement';
import Settings from '../Containers/Settings';
import DataManager from '../Containers/DataManager';
import ContactUs from '../Containers/ContactUs';

const topStack = StackNavigator({
  main: { screen: MainScreen },
})

// drawer stack
const PrimaryNav = DrawerNavigator({
  top: { screen: topStack },
  account: { screen: AccountManagement },
  settings: { screen: Settings },
  datamanager: { screen: DataManager },
  contact: { screen: ContactUs },
}, {
    gesturesEnabled: true,
    contentComponent: SideMenu,
    headerMode: 'screen',
  })

export default PrimaryNav  

I am using my own component to render the Drawer, but switching to the default contentComponent does not seem to make any difference. 我正在使用自己的组件来渲染Drawer,但是切换到默认的contentComponent似乎没有任何区别。

ReduxNavigation.js ReduxNavigation.js

import React from 'react'
import * as ReactNavigation from 'react-navigation'
import { connect } from 'react-redux'
import AppNavigation from './AppNavigation'

function ReduxNavigation(props) {
  const { dispatch, nav } = props
  const navigation = ReactNavigation.addNavigationHelpers({
    dispatch,
    state: nav
  })

  return <AppNavigation navigation={navigation} />
}

const mapStateToProps = state => ({ nav: state.nav })
export default connect(mapStateToProps)(ReduxNavigation)

Finally, here my App.js file 最后,这里是我的App.js文件

import React from 'react'
import { StyleSheet, View, StatusBar } from 'react-native'
import { Provider } from 'react-redux'
import createStore from './Redux/Reducers'
import ReduxNavigation from './Navigation/ReduxNavigation'

const store = createStore()

export default class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <View style={styles.container}>
          <StatusBar barStyle='light-content' />
          <ReduxNavigation />
        </View>
      </Provider>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff'
  },
})

There is no value providing the individual screens, they are simply placeholders with a few buttons to exercise the prototype. 提供单个屏幕没有任何价值,它们只是带有几个按钮的占位符,用于执行原型。

Any help or clarification would be greatly appreciated. 任何帮助或澄清将不胜感激。

I guess it is always embarrassing when you figure out that it all was an avoidable oversight. 我想当您发现所有这些都是可以避免的疏忽时,总是令人尴尬。 I had totally forgotten that Expo controls screen orientation and you have to explicitly enable specific orientation choices. 我完全忘记了Expo可以控制屏幕方向,而您必须明确启用特定的方向选择。 When I built my prototype, I oversimplified and forgot to include the Expo 'ScreenOrientation' directive. 在构建原型时,我简化了很多,忘了包含Expo'ScreenOrientation'指令。

Here the corrected App.js: 这里是更正的App.js:

import React from 'react'
import { StyleSheet, View, StatusBar } from 'react-native'
import { Provider } from 'react-redux'
import createStore from './Redux/Reducers'
import { ScreenOrientation } from 'expo'; // <==== added this line

const store = createStore()

export default class App extends React.Component {
  render() {
    ScreenOrientation.allow(ScreenOrientation.Orientation.ALL); // <==== added this line
    return (
      <Provider store={store}>
        <View style={styles.container}>
          <StatusBar barStyle='light-content' />
          <ReduxNavigation />
        </View>
      </Provider>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff'
  },
})

At least it only took me two hours to find the answer, hopefully this will save someone else two hours ... 至少我只花了两个小时就找到了答案,希望这可以为别人节省两个小时...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM