uni-app 微信小程序中 uni.getSystemInfoSync() 获取信息错误
uni-app 微信小程序中 uni.getSystemInfoSync() 获取信息错误
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Mac | 11.6 | HBuilderX |
| HBuilderX | 3.2.9 | |
| 工具 | 1.05.2110290 | |
| 基础库 | 2.19.2 |
操作步骤:
console.log(uni.getSystemInfoSync().safeAreaInsets.bottom)
### 预期结果:
结果值应该为34
实际结果:
-54
更多关于uni-app 微信小程序中 uni.getSystemInfoSync() 获取信息错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html
报bug几天没人管的吗????
更多关于uni-app 微信小程序中 uni.getSystemInfoSync() 获取信息错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html
感谢反馈。
在小程序上 safeAreaInsets 是由 safeArea 的值与 window 信息计算而来。
之前推断 safeArea 的值是相对于 window 定义的,当时没有 screenTop,也只能相对于 window 计算。
从目前打印的结果来看原始值 safeArea.height 值大于了 windowHeight,可能是相对于 screen 计算而来。
后续会进行测试以及联系小程序社区来确定小程序 safeArea 的定义方式,以便对 safeAreaInsets 进行纠正。
预记下版修复
HBuilderX alpha 3.4.8+ 已修复
这bug终于解决了 , 我都是用 screenHeight - safeArea.bottom
我觉得你这个是个好办法,而且老项目不能更新只能用这种办法
在微信小程序中,uni.getSystemInfoSync().safeAreaInsets.bottom 返回负值(如 -54)通常是因为该设备不支持安全区域(safeArea)概念,或系统版本较低。safeAreaInsets 主要用于全面屏设备,表示屏幕边缘的安全区域。在不支持的设备上,该值可能为负或 0。
解决方案:
-
检查设备支持性:使用
uni.getSystemInfoSync()获取完整信息,确认safeArea或safeAreaInsets是否存在。例如:const systemInfo = uni.getSystemInfoSync(); console.log('safeAreaInsets:', systemInfo.safeAreaInsets);如果返回
undefined或null,说明设备不支持。 -
降级处理:对于不支持安全区域的设备,可以回退到使用
screenHeight和windowHeight计算底部安全距离。例如:const systemInfo = uni.getSystemInfoSync(); const safeBottom = systemInfo.safeAreaInsets ? systemInfo.safeAreaInsets.bottom : 0; // 或使用:const safeBottom = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight; -
更新基础库:确保微信开发者工具和真机的基础库版本为最新(如 2.21.0+),以支持安全区域 API。
-
真机测试:在真机上验证,因为模拟器可能无法准确模拟安全区域。
代码示例:
const systemInfo = uni.getSystemInfoSync();
let safeBottom = 0;
if (systemInfo.safeAreaInsets) {
safeBottom = systemInfo.safeAreaInsets.bottom;
} else {
// 降级计算(适用于非全面屏)
safeBottom = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight;
}
console.log('safeBottom:', safeBottom);

