小程序建议可以获取titleBar高度的需求。
发布于 4 年前 作者 jie53 8743 次浏览 来自 问答

做自定义导航栏用。

需要获取用户机型导航栏高度。

wx.getSystemInfo({

      success: (res) => {

        // let totalTopHeight = (wx.getMenuButtonBoundingClientRect().bottom + wx.getMenuButtonBoundingClientRect().top) - (res.statusBarHeight * 2)

        // this.globalData.statusBarHeight = res.statusBarHeight

        // this.globalData.titleBarHeight = totalTopHeight

        // console.log(wx.getMenuButtonBoundingClientRect())   //真机上top 和 bottom 少2px 开发者工具正常。该方法拿到的高度在iPad上还是无法做到适配。

//该方法不兼容安卓刘海屏。

        let totalTopHeight = 68

        if (res.model.indexOf(‘iPhone X’) !== -1) {

          totalTopHeight = 88

        } else if (res.model.indexOf(‘iPhone’) !== -1) {

          totalTopHeight = 64

        }

        this.globalData.statusBarHeight = res.statusBarHeight

        this.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight

        console.log(this.globalData.statusBarHeight)

        console.log(this.globalData.titleBarHeight)

      },

      fail: () => {

        this.globalData.statusBarHeight = 20

        this.globalData.titleBarHeight = 44

      }

    })

迫切的希望能通过api拿到titleBarHeight:也就是这块区域的高度。用胶囊位置去算 真机上top 和 bottom 少2px;


代码片段如下:https://developers.weixin.qq.com/s/wi6Pglmv7s8P。

2 回复

追问一下,wx.getMenuButtonBoundingClientRect()在苹果机时常获取不到是什么情况?

顶一个,我也遇到了这个问题

回到顶部