39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import Taro from "@tarojs/taro";
|
|
|
|
export const getNavbarHeight = (): { statusBarHeight: number; navBarHeight: number; totalHeight: number; } => {
|
|
// 1. 获取系统信息,包含状态栏高度
|
|
const systemInfo = Taro.getSystemInfoSync();
|
|
const statusBarHeight = systemInfo.statusBarHeight || 0;
|
|
|
|
|
|
const isIOS = systemInfo.platform === "ios";
|
|
const isIPad = systemInfo.model?.toLowerCase().includes('ipad') ||
|
|
(systemInfo.platform === 'ios' && systemInfo.screenWidth >= 768);
|
|
|
|
// 2. 获取胶囊按钮位置信息(用于计算导航栏高度)
|
|
const menuButtonInfo = Taro.getMenuButtonBoundingClientRect();
|
|
|
|
let navBarHeight = 44 // 默认导航栏高度
|
|
|
|
if (menuButtonInfo && menuButtonInfo.top) {
|
|
// 计算导航栏高度公式
|
|
navBarHeight = (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height
|
|
} else {
|
|
// 胶囊按钮信息不可用时,使用默认值
|
|
if (isIPad) {
|
|
navBarHeight = 50; // iPad 上的导航栏高度通常更高
|
|
} else if (isIOS) {
|
|
navBarHeight = 44; // iPhone 导航栏高度
|
|
} else {
|
|
navBarHeight = 48; // Android 导航栏高度
|
|
}
|
|
}
|
|
|
|
const totalHeight = statusBarHeight + navBarHeight;
|
|
|
|
return {
|
|
statusBarHeight,
|
|
navBarHeight,
|
|
totalHeight,
|
|
};
|
|
}; |