如何动态设置推送屏幕的方向 - React-Native-Navigation

Sea*_*son 1 react-native react-native-navigation

我想知道如何使用 react-native-navigation 动态更改推送屏幕的方向。

我使用 startSingleScreenApp 和 appStyle oforientation: 'portrait' 强制纵向屏幕。我只想让推送的屏幕之一使用“自动”

我尝试了以下方法:

static navigatorStyle = {
   drawUnderTabBar: true,
   orientation: 'auto'
};
Run Code Online (Sandbox Code Playgroud)

我也试过:

componentWillMount() {
   this.props.navigator.setStyle({
      orientation: 'auto'
   });
};
Run Code Online (Sandbox Code Playgroud)

我什至使用以下方法推送到屏幕上:

...
navigatorStyle: {
  orientation:'auto'
}
...
Run Code Online (Sandbox Code Playgroud)

甚至可以动态更改它吗?

San*_*ndy 5

它可以通过使用“React Native Orientation”来实现。它将侦听 React Native 应用程序中的设备方向变化,并以编程方式在每个屏幕的基础上设置首选方向。它适用于 Android 和 iOS。请找到以下链接了解更多详情:

https://github.com/yamill/react-native-orientation
Run Code Online (Sandbox Code Playgroud)

您可以按如下方式使用它:

import Orientation from 'react-native-orientation';

// this locks the view to Portrait Mode. Need to add this in your screens 
 componentDidMount function
Orientation.lockToPortrait();
Run Code Online (Sandbox Code Playgroud)

或者

// this locks the view to Landscape Mode. Need to add this in your screens 
 componentDidMount function
 Orientation.lockToLandscape();
Run Code Online (Sandbox Code Playgroud)

您可以在共享链接中找到详细说明/指南。您可以根据需要相应地添加逻辑。希望它会帮助你。