小程序 状态栏、导航栏高度 胶囊按钮尺寸定位

  • 自定义导航栏时,考虑到状态栏的固有高度,以及胶囊按钮的位置,需要对自定义的导航栏设定相仿的尺寸、位置。

已有的小程序 API 包括 状态栏高度、胶囊按钮信息:

wx.getSystemInfo()
wx.getMenuButtonBoundingClientRect()

通过 wx.getSystemInfo() 返回的 statusBarHeight ,即为状态栏高度。再通过 wx.getMenuButtonBoundingClientRect()获取到胶囊按钮的宽高以及上下边界的坐标,即可计算出导航栏高度。


wx.getSystemInfo({
  success: res => {
    // 状态栏高度
    let statusBarHeight = res.statusBarHeight;

    let menuButtonRect = wx.getMenuButtonBoundingClientRect()
    // 导航栏高度
    let navigationBarHeight = (menuButtonRect.top - statusBarHeight) * 2 + menuButtonRect.height
  }
});

原文地址:https://www.cnblogs.com/_error/p/14510409.html