react-native - 如何使用 React Native Stack Navigation 处理锁定的方向
问题描述
我有一个在 iOS 和 Android 上运行的 React Native Expo 应用程序,使用 Stack Navigation 和两个视图。第一个视图被锁定为纵向屏幕方向
export class HomeScreen extends Component {
componentWillMount() {
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
}
render() {...
}
}
第二个视图应该在纵向和横向屏幕方向都可用:
export class DetailScreen extends Component {
componentDidMount() {
ScreenOrientation.lockAsync(
ScreenOrientation.OrientationLock.ALL_BUT_UPSIDE_DOWN
);
}
async componentWillUnmount() {
await ScreenOrientation.lockAsync(
ScreenOrientation.OrientationLock.PORTRAIT_UP
);
}
render() {...
}
}
这几乎按预期工作,但我有两个问题:
- 当第二个视图 (
DetailScreen
) 处于横向并按下返回按钮时,第一个视图 (HomeScreen) 会在旋转回纵向之前短暂地以横向显示。是否可以确保设备在返回之前已旋转为纵向?我尝试在内部async await
的componentWillUnmount
方法中使用它DetailScreen
,但是当组件被卸载时屏幕仍然是横向的。
- 使用手势我可以导航回
HomeScreen
.DetailScreen
但是当以横向显示时,执行此手势,也以横向HomeScreen
显示。我该如何处理?是否有可能以某种方式DetailScreen
在横向时禁用此手势?
该示例可在此 Expo Snack 中找到: https ://snack.expo.io/HJ_nhkQKH
解决方案
我为您的问题更新了世博小吃: https ://snack.expo.io/BJfXseXYB
第 1 部分。使用 NavigationEvents - onWillFocus
第2部分:
const RootStack = createStackNavigator(
{
Home: {
screen: HomeScreen,
navigationOptions: {
gesturesEnabled: true,
},
},
DetailView: {
screen: DetailScreen,
navigationOptions: {
gesturesEnabled: false,
},
},
},
{
initialRouteName: 'Home',
}
);
推荐阅读
- c# - 如何从动态创建的文本框中获取文本以便存储在数据库中?
- android - 从 AsyncTask 获取返回
- java - 放置freemarker模板文件的最佳做法是什么
- android - 无法在阵列适配器内的 View Pager 中添加视图或膨胀
- python - 如何检查 nan 中的熊猫列值,这些值是 python 中的列表
- javascript - javascript调用递归函数时堆栈何时耗尽?
- opencv - 如何从单个组件创建 2D 透视变换矩阵?
- html - 触发:悬停在移动元素上而不移动鼠标
- algorithm - 如何实现 Btree 的迭代器?
- laravel - 在laravel错误中获取除图像之外的所有数据