- 自定義導航欄時,考慮到狀態欄的固有高度,以及膠囊按鈕的位置,需要對自定義的導航欄設定相仿的尺寸、位置。
已有的小程序 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
}
});