小程序自定义导航栏,实现与胶囊对齐功能

注意screenHeight屏高:屏幕高度 = 原生NavigationBar高度(含状态栏高度)+ 可使用窗口高度 + 原生TabBar高度
但是自定义导航栏screenHeight=windowHeight

由于是自定义导航栏无法获取原生NavigationBar高度,需要手动计算。
首先先获取状态栏高度和右上角胶囊的布局位置信息:
分别是uni.getSystemInfo()获取系统信息以及uni.getMenuButtonBoundingClientRect(这个函数h5环境下不能用要使用条件编译
在这里插入图片描述
胶囊的顶部坐标是相对于屏幕,则由上图可知,标题栏高度 = (胶囊的顶部坐标 - 信号栏高度 )* 2 + 胶囊高度
在这里插入图片描述
最终代码

data() {
			return {
				menuButtonInfo:0,//胶囊按钮信息
				statusBarHeight:0,//状态栏高度
				musicheadHeight:0
			};
},
onReady() {
			// #ifdef  MP-WEIXIN
			//获取状态栏高度
			const {	statusBarHeight,windowHeight,screenHeight }= uni.getSystemInfoSync()
			this.statusBarHeight=statusBarHeight
			// 获取胶囊按钮信息(width、height、top等)
			const {width,height,top}=uni.getMenuButtonBoundingClientRect()
			this.menuButtonInfo={width,height,top}
			// 胶囊按钮相对于离导航栏的上边距
			const topDistance=this.menuButtonInfo.top-this.statusBarHeight;
			// 计算导航栏高度
			this.musicheadHeight=this.menuButtonInfo.height+topDistance*2;
			 // #endif
		},

布局时使用status_bar占位块代替状态栏,不然后面用line-height设置标题居中有点麻烦,而且不可以使用height: var(–status-bar-height)设置状态栏高度(这个好像是固定的25px,而刘海屏已经不是25px了),必须动态设置。

<view class="musichead">
		<view class="status_bar"  :style="{'height':statusBarHeight+'px'}"></view>
		<!-- #ifdef MP-WEIXIN -->
		<view class="musicheadWEIXIN" :style="{
			'height':musicheadHeight+'px',
			'line-height': musicheadHeight+'px'
			}">
			<!-- 左边按钮 -->
			<view class="btn" :style="{
				'width':menuButtonInfo.width+'px',
				'height':menuButtonInfo.height+'px',
				'line-height':menuButtonInfo.height+'px',
				'top':(menuButtonInfo.top)+'px'
				}"
			>
				<view class="iconfont icon-zuojiantou"></view>
				<view class="split_line">|</view>
				<view class="iconfont icon-shouye"></view>
			</view>
			<!-- 标题 -->
			<text class="title">{{title}}</text>
		</view>
		<!-- #endif -->
	</view>
.musicheadWEIXIN{
	width: 100%;
	padding: 0;
	margin: 0;
	text-align: center;
}
.status_bar {
    // height: var(--status-bar-height);
    width: 100%;
}
.btn{
	position: absolute;
	display: flex;
	box-sizing: border-box;
	padding: 0;
	margin: 0;
	justify-content: space-around;
	border: 1px solid #dcdcdc;
	border-radius: 20px;
	left:10px;
	background-color: #fff;
	.split_line{
		color: #ffe8e8;
	}
}

最终结果
在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现自定义导航栏与微信胶囊按钮对齐,可以按照以下步骤进行: 1. 首先在 `App.vue` 中设置导航栏的高度为微信胶囊按钮的高度,即44px。可以使用 `uni.getSystemInfoSync()` 获取系统信息,再根据 `statusBarHeight` 和 `menuButtonBoundingClientRect.height` 计算出导航栏的高度。 ``` <template> <view class="navigation-bar" :style="{ height: navigationBarHeight + 'px' }"> <!-- 自定义导航栏内容 --> </view> </template> <script> export default { data() { return { navigationBarHeight: 0 } }, mounted() { const systemInfo = uni.getSystemInfoSync() const { statusBarHeight, menuButtonBoundingClientRect } = systemInfo this.navigationBarHeight = statusBarHeight + menuButtonBoundingClientRect.height } } </script> <style> .navigation-bar { position: fixed; top: 0; left: 0; right: 0; background-color: #fff; z-index: 999; } </style> ``` 2. 接下来,在页面中使用 `uni.getMenuButtonBoundingClientRect()` 获取微信胶囊按钮的位置信息,然后根据位置信息计算出导航栏左侧和右侧的边距。 ``` <template> <view class="navigation-bar" :style="{ height: navigationBarHeight + 'px', paddingLeft: paddingLeft + 'px', paddingRight: paddingRight + 'px' }"> <!-- 自定义导航栏内容 --> </view> </template> <script> export default { data() { return { navigationBarHeight: 0, paddingLeft: 0, paddingRight: 0 } }, mounted() { const systemInfo = uni.getSystemInfoSync() const { statusBarHeight, menuButtonBoundingClientRect } = systemInfo this.navigationBarHeight = statusBarHeight + menuButtonBoundingClientRect.height const { left, right } = uni.getMenuButtonBoundingClientRect() this.paddingLeft = left this.paddingRight = systemInfo.windowWidth - right } } </script> <style> .navigation-bar { position: fixed; top: 0; left: 0; right: 0; background-color: #fff; z-index: 999; } </style> ``` 通过以上步骤,就可以实现自定义导航栏与微信胶囊按钮对齐了。需要注意的是,如果页面中使用了 `uni.setNavigationBarTitle()` 等原生导航栏相关方法,需要自行计算出对应的边距。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值