uni-app中getSystemInfoSync失效
uni-app中getSystemInfoSync失效
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Mac | 10.15.6 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Mac
PC开发环境操作系统版本号:10.15.6
HBuilderX类型:Alpha
HBuilderX版本号:3.2.6
手机系统:Android
手机系统版本号:Android 10
手机厂商:华为
手机机型:华为 meta 30
页面类型:vue
打包方式:云端
### 示例代码:
```javascript
let getSystem = uni.getSystemInfoSync()
let statusBarHeight = getSystem.statusBarHeight;
console.log(statusBarHeight)
uni.getSystemInfo({
success: function (res) {
console.log(res.statusBarHeight);
}
});
操作步骤:
- 使用上面代码,打印安卓状态栏高度
预期结果:
- 使用上面代码,打印安卓状态栏高度 为 正常状态栏高度
实际结果:
- 使用上面代码,打印安卓状态栏高度 为 0
- 打印结果: 0 at components/headerTop/headerTop.vue:167
bug描述:
api getSystemInfoSync 获取不到安卓状态栏高度 同步异步都不行
更多关于uni-app中getSystemInfoSync失效的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
uni.getSystemInfoSync() 在特定环境下获取 statusBarHeight 为 0,通常与运行环境或时机有关。
排查与解决步骤:
-
检查运行环境:
statusBarHeight在 App 端(渲染在 Webview 中)和 H5 端均可获取。请确认代码在 App 端 执行,而非浏览器或小程序模拟器。在onLoad或onReady生命周期中调用可能过早,建议在onShow或mounted(Vue 组件)中调用。 -
使用异步 API 兜底:同步方法可能因环境未完全初始化返回异常值。可优先使用异步
uni.getSystemInfo()获取,并在其回调中处理逻辑:uni.getSystemInfo({ success: (res) => { console.log('异步获取 statusBarHeight:', res.statusBarHeight); this.statusBarHeight = res.statusBarHeight; // 赋值到数据中 } }); -
检查容器与时机:在自定义组件或非页面组件中,确保调用时机在页面渲染完成后。可尝试加延时验证:
setTimeout(() => { let sys = uni.getSystemInfoSync(); console.log('延时获取:', sys.statusBarHeight); }, 100);


