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

How to dynamically set orientation of pushed screen - React-Native-Navigation

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

我正在使用具有方向 appStyle 的 startSingleScreenApp:'portrait' 强制纵向屏幕。我只想使用其中一个推送屏幕 'auto'

我试过以下方法:

static navigatorStyle = {
   drawUnderTabBar: true,
   orientation: 'auto'
};

我也试过:

componentWillMount() {
   this.props.navigator.setStyle({
      orientation: 'auto'
   });
};

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

...
navigatorStyle: {
  orientation:'auto'
}
...

甚至可以动态更改它吗?

使用"React Native Orientation"即可实现。它将监听 React Native 应用程序中的设备方向变化,并以编程方式在每个屏幕的基础上设置首选方向。它适用于 Android 和 iOS。请在下面 link 找到更多详细信息:

https://github.com/yamill/react-native-orientation

您可以按如下方式使用:

import Orientation from 'react-native-orientation';

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

或者

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

您可以在共享 link 中找到详细的 instructions/guides。您可以根据需要相应地添加逻辑。希望对你有所帮助。