【问题标题】:React Native - Modal - Dynamic Max HeightReact Native - 模态 - 动态最大高度
【发布时间】:2021-06-19 03:32:42
【问题描述】:

我在视图中使用模式 - 它包含一个表单。表单比视口长 - 因此,内容占据了页面的高度并滚出视图。

谁能建议动态高度的最佳方法?

目前我正在使用以下方法,但如果手机方向切换则不起作用,我确定必须有更好的解决方案?

  heightScreen = () => {
    return Dimensions.get('window').height - 150;
  }  



<Modal
    isVisible={this.props.showModal}
    animationInTiming={500}
    backdropColor={'#f79431'}
    style={{  marginVertical:50, maxHeight: this.heightScreen()}}
   >

【问题讨论】:

    标签: reactjs react-native react-native-modal


    【解决方案1】:

    import {useEffect, useState} from 'react';
    import {Dimensions} from 'react-native';
    
    export const useOrientation = () => {
      const [orientation, setOrientation] = useState("PORTRAIT");
    
      useEffect(() => {
        Dimensions.addEventListener('change', ({ window:{ width, height } }) => {
            setOrientation(width < height ? "PORTRAIT" : "LANDSCAPE")
        })
      }, []);
    
      return orientation;
    }

    您可以添加这个函数作为辅助来检测方向(纵向/横向),并在此基础上应用正确的高度。

    【讨论】:

    • 非常感谢您的帮助@Nicolae - 但是上面的代码不会返回一个数字 - (即身高数字),​​除非我错过了什么?干杯保罗
    猜你喜欢
    • 2020-08-07
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多