react-native - 在 React Native 中,模态不会旋转到横向屏幕?
问题描述
Modal
我在旋转到横向屏幕时遇到问题。我试图弄清楚我做错了什么,当我第一次打开模态时,模态会旋转,但是如果我再次尝试打开它并旋转手机,模态不会旋转。我创建了两个函数unlockScreenToDefault
和lockScreenToPortraitOrientation
:
const lockScreenToPortraitOrientation = async () => {
await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT);
};
const unlockScreenToDefault = async () => {
await ScreenOrientation.unlockAsync();
};
我有这样的代码:
模态组件
export default class ModalPhoto extends Component {
componentDidMount() {
unlockScreenToDefault().then();
}
componentWillUnmount() {
lockScreenToPortraitOrientation().then();
}
render() {
const {
visible,
onClose,
source,
type,
} = this.props;
return (
<Modal
animationType="fade"
visible={visible}
transparent
onRequestClose={() => {
console.log('Modal has been closed.');
}}
supportedOrientations={
[
'portrait',
'portrait-upside-down',
'landscape',
'landscape-left',
'landscape-right'
]
}
>
<View style={styles.modalContainer}>
<ImageViewer
imageUrls={[{
height,
width,
props: {
source
}
}]}
/>
<TouchableOpacity
style={styles.back}
onPress={() => {
lockScreenToPortraitOrientation().then(() => onClose());
}}
>
<Text style={styles.label}>{Translations.translate('Back')}</Text>
</TouchableOpacity>
</View>
</Modal>
);
}
}
我在我的代码中使用该Modal
组件,如下所示:
pictures.map(picInfo => (
<View key={picInfo.uri}>
{ country !== 'US' && (
<View>
<TouchableOpacity
onPress={() => {
this.setState(oldState => ({
modalPictureVisible: !oldState.modalPictureVisible,
modalUri: picInfo.uri
}));
}}
>
<Image key={picInfo.uri} style={styles.setPicture} source={{ uri: picInfo.uri }} />
</TouchableOpacity>
<ModalPhoto
visible={modalPictureVisible}
source={{ uri: modalUri }}
onClose={() => this.setState({ modalPictureVisible: false, modalUri: '' })}
/>
</View>
)}
</View>
));
解决方案
推荐阅读
- c# - C# Post,Webrequest 循环不工作,循环 i = 2 然后没有结果
- sql - 如何获得仅按最后日期选择的结果?
- c - 在 C 中格式化文本
- javascript - 每次 var 更改后更新 res.locals?
- php - 为 foreach() -Laravel 提供的参数无效
- spring-mvc - 如何在 Thymeleaf 每个循环中编写条件表达式?
- jmeter - jmeter http 标头管理器授权字段“错误的身份验证数据”
- nlp - 创建单词字典并映射到其他语言
- excel - 无法使用 vba 从字符串中提取金额
- javascript - 在 Phaser js 3 上获取输入持续时间