小程序 wx.getSystemInfoSync 获取 windowHeight 问题

windowHeight 概念

可以使用窗口高度,即:屏幕高度(screenHeight) - 导航(tabbar)高度异步

存在问题

安卓设备下获取 windowHeight 不能准确获得对应的高度,老是拿到屏幕高度code

缘由

1. 同步接口 wx.getSystemInfoSync 并不一样步(猜想)

wx.getSystemInfoSync 只是在页面初始化时提早计算。因此对于 windowHeight 这种须要进行功能判断的属性,应该使用异步接口,实时获取接口

2. wx.getSystemInfo 调用的时机不当

上面讲了 windowHeight 的定义,因此这个值取决于 tabbar 是否存在生命周期

为了保证 tabbar 显示后再进行取值,建议在生命周期的 onReady 钩子中调用接口 wx.getSystemInfoget

最终方案

  1. 采用异步接口 wx.getSystemInfo
  2. onReady 中调用

代码同步

onReady() {
    wx.getSystemInfo({
      success({windowHeight}) {
        // todo
      }
    });
  }
相关文章
相关标签/搜索