【发布时间】:2021-05-05 19:47:10
【问题描述】:
我正在尝试使用 expo 在 react native 中添加旋转功能。遵循教程并在谷歌和这里搜索,但不知何故,它们都没有工作。旋转模拟器时未检测到屏幕尺寸。因此,它不会从纵向变为横向,反之亦然。 您可以看到下面我尝试通过一个工作示例尝试的代码,但是当我应用它时它不起作用。非常感谢任何帮助。
const [orientation, setOrientation] = useState("");
const window = useWindowDimensions();
useEffect(() => {
Dimensions.addEventListener("change", ({ window: { width, height } }) => {
if (width < height) {
// this is in portrait mode
setOrientation("Portrait");
} else {
// this is in landscape mode
setOrientation("Landscape");
}
});
}, []);
return (
<View style={styles.container}>
<Text style={{ color: "red", fontSize: 30, fontWeight: "bold" }}>
{orientation}
</Text>
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
});
【问题讨论】:
-
如果你在使用 expo 为什么不使用它呢? docs.expo.io/versions/latest/sdk/screen-orientation