uniapp顶部导航栏高度适配

为了实现好看又实用的顶部导航栏,不得不自己定义导航栏样式。而自己定义的导航栏高度会因为手机的型号不同所展示的效果也就不同,所以只能通过适配高度来达到预期的效果

1.需要在page.json文件中对需要自定义导航栏文件进行配置

"navigationStyle":"custom" //取消默认的原生导航栏

2.在页面中添加占位元素

html 复制代码
		<!-- 状态栏高度 -->
<view :style="{ height: `${statusBarHeight}px` }"></view>

 <!-- 自定义导航栏高度  -->
        <view :style="{
          height: `${barHeight}px`,
          'line-height': `${barHeight}px`
        }">
            <text>评价</text>
        </view>

3.借助钩子函数mounted完成逻辑处理

javascript 复制代码
	mounted() {
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
			const {
				top,
				height
			} = wx.getMenuButtonBoundingClientRect();
			// 自定义导航栏高度 = 胶囊高度 + 胶囊的padding*2, 如果获取不到设置为38
			this.barHeight = height ? height + (top - this.statusBarHeight) * 2 : 38;
		},

这样就行了,不同的设备导航栏高度就会自适应了

相关推荐
NiceCloud喜云5 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
wordbaby6 小时前
React Native + RNOH:跨页面数据回传的最佳实践与避坑指南
前端·react native
GISer_Jing6 小时前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
丷丩6 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
油炸自行车6 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
Front思7 小时前
AI前端工程师需要具备能力+
前端·人工智能·ai
ZC跨境爬虫9 小时前
跟着 MDN 学CSS day_29:(掌握文本与字体样式的核心艺术)
前端·css·ui·html·tensorflow
李子琪。10 小时前
网络空间安全深度实战:CSRF 漏洞原理剖析与基于 Token 的纵深防御体系构建(全栈实验报告)
前端·安全·csrf
冰暮流星10 小时前
javascript之history对象介绍
前端·笔记
IT_陈寒10 小时前
Vite热更新失灵?你可能漏了这个配置
前端·人工智能·后端