记录--优雅解决uniapp微信小程序右上角胶囊菜单覆盖问题
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助
前言
大家好,今天聊一下在做uniapp多端适配项目,需要用到自定义导航时,如何解决状态栏塌陷及导航栏安全区域多端适配问题,下文只针对H5、APP、微信小程序三端进行适配,通过封装一个通用高阶组件包裹自定义导航栏内容,主要是通过设置padding来使内容始终保持在安全区域,达到低耦合,可复用性强的效果。
一、创建NavbarWrapper.vue组件
大致结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <template> <view class = "navbar-wrapper" :style="{ paddingTop: statusBarHeight, paddingRight: rightSafeArea }"> <slot/> </view> </template> <script> export default { name: 'NavbarWrapper' , data() { return { // 像素单位 pxUnit: 'px' , // 默认状态栏高度 statusBarHeight: 'var(--status-bar-height)' , // 微信小程序右上角的胶囊菜单宽度 rightSafeArea: 0 } } } </script> <style scoped> .navbar-wrapper { /** * 元素的宽度和高度包括了内边距(padding)和边框(border), * 而不会被它们所占据的空间所影响 * 子元素继承宽度时,只会继承内容区域的宽度 */ box-sizing: border-box; } </style> |
目的
主要是动态计算statusBarHeight和rightSafeArea的值。
解决方案
在APP端只需一行css代码即可
1 2 3 | .navbar-wrapper { padding-top: var (--status-bar-height); } |
下面是关于--status-bar-height
变量的介绍:
从上图可以知道--status-bar-height
只在APP端是手机实际状态栏高度,在微信小程序是固定的25px
,并不是手机实际状态栏高度;
在微信小程序时,除了状态栏高度还需要获取右上角的胶囊菜单所占宽度,保持导航栏在安全区域。
以下使用uni.getWindowInfo()
和uni.getMenuButtonBoundingClientRect()
来分别获取状态栏高度和胶囊相关信息,api介绍如下图所示:
主要逻辑代码
在NavbarWrapper组件创建时,做相关计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | created() { const px = this .pxUnit // #ifndef H5 // 获取窗口信息 const windowInfo = uni.getWindowInfo() this .statusBarHeight = windowInfo.statusBarHeight + px // #endif // #ifdef MP-WEIXIN // 获取胶囊左边界坐标 const { left } = uni.getMenuButtonBoundingClientRect() // 计算胶囊(包括右边距)占据屏幕的总宽度:屏幕宽度-胶囊左边界坐标 this .rightSafeArea = windowInfo.windowWidth - left + px // #endif } |
用法
1 2 3 | <NavbarWrapper> <view class = "header" >header</view> </NavbarWrapper> |
二、多端效果展示
微信小程序
APP端
H5端
三、源码
NavbarWrapper.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <template> <view class = "navbar-wrapper" :style="{ paddingTop: statusBarHeight, paddingRight: rightSafeArea }"> <slot/> </view> </template> <script> export default { name: 'NavbarWrapper' , data() { return { // 像素单位 pxUnit: 'px' , // 默认状态栏高度 statusBarHeight: 'var(--status-bar-height)' , // 微信小程序右上角的胶囊菜单宽度 rightSafeArea: 0 } }, created() { const px = this .pxUnit // #ifndef H5 // 获取窗口信息 const windowInfo = uni.getWindowInfo() this .statusBarHeight = windowInfo.statusBarHeight + px // #endif // #ifdef MP-WEIXIN // 获取胶囊左边界坐标 const { left } = uni.getMenuButtonBoundingClientRect() // 计算胶囊(包括右边距)占据屏幕的总宽度:屏幕宽度-胶囊左边界坐标 this .rightSafeArea = windowInfo.windowWidth - left + px // #endif } } </script> <style scoped> .navbar-wrapper { /** * 元素的宽度和高度包括了内边距(padding)和边框(border), * 而不会被它们所占据的空间所影响 * 子元素继承宽度时,只会继承内容区域的宽度 */ box-sizing: border-box; background-color: deeppink; } </style> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2022-12-08 记录--微信小程序获取用户信息(附代码、流程图)
2020-12-08 uni-app开发经验分享十四:小程序超过2M限制的方法——分包加载
2020-12-08 uni-app开发经验分享十三:实现手机扫描二维码并跳转全过程