【发布时间】:2018-02-05 11:59:49
【问题描述】:
我在我的应用程序中使用了TabNavigation。在某些屏幕上,我希望它只显示在portrait 方向上,而其他屏幕是landscape。我找到了react-native-orientation 并在我的屏幕上尝试:
屏幕 1
import React from "react";
import Orientation from 'react-native-orientation';
import { Text } from 'react-native'
export default Screen1 extends React.PureComponent{
componentDidMount(){
Orientation.lockToLandscape();
}
componentWillUnmount(){
Orientation.unlockAllOrientations();
}
render() {
return(<Text>Screen 1</Text>);
}
}
屏幕 2
import React from "react";
import Orientation from 'react-native-orientation';
import { Text } from 'react-native'
export default Screen2 extends React.PureComponent{
componentDidMount(){
Orientation.lockToPortrait();
}
componentWillUnmount(){
Orientation.unlockAllOrientations();
}
render() {
return(<Text>Screen 2</Text>);
}
}
标签导航器
const AppNavigator = TabNavigator( {
Screen1: { screen: Screen1 },
Screen2: { screen: Screen2 },
});
但它总是纵向的,这意味着它的方向总是基于我在TabNavigator 添加的最后一个屏幕的方向。任何帮助表示感谢,在此先感谢您!
编辑 1
我尝试了StackNavigation,它可以工作。仍然不知道为什么它没有与TabNavigation 一起运行。
【问题讨论】:
-
尝试在componentWillMount而不是componentDidMount中锁定方向,可能是因为挂载无法锁定方向。
-
@MtgKhaJeskai 我试过了,但还是不行,立即重新更改为最后一个屏幕的方向。
-
您是否检查过 TabNavigator 中的 Screen1 和 Screen2 是否实际上正在卸载以便解锁方向?
-
@Unicorn Master 这似乎是导致我的错误的原因,您有什么建议让我解决它吗?
标签: react-native react-navigation