首页 > 解决方案 > React Native Dimensions iOS Change 事件不会触发

问题描述

我的 componentDidMount 中有一个非常简单的事件监听器来监听尺寸的变化,这在我的 iPhone 上的 iOS 模拟器中永远不会触发(多个模型),但它确实会在 iPad 模拟器上触发并且在 Android 中运行良好。

componentDidMount() {

    Dimensions.addEventListener( 'change', () =>
       {
         this.getOrientation();
       });

}
getOrientation = () => {
  alert("Trigger Called"); // never fires in iPhone Simulator, but works on iPad Simulator and Android

  if (Dimensions.get("screen").width < Dimensions.get("screen").height) {
    this.setState({ screenOrientation: "portrait" });
  } else {
    this.setState({ screenOrientation: "landscape" });
  }
};

难道我做错了什么?我也尝试过使用 EXPO Orientation,但遇到了完全相同的问题。

谢谢

标签: reactjsreact-nativeexpo

解决方案


我发现了我的问题,似乎从 react-native-elements 使用 Overlay 时,它不会触发 change 事件。删除它修复它在 iPhone X(s)


推荐阅读