uniapp 自定义导航栏和右上角胶囊按钮对齐

自定义导航栏和右上角胶囊按钮对齐,需要分俩步走,一个是在上面搞个填充区用来填充刘海区域即statusBar, 然后计算出来胶囊区域的高度,让我们自己的导航栏内容高度一致,再css居中。

在这里插入图片描述

  1. 填充区,高度为statusBar的高度,下方代码class为“fill”,不需要内容,只是起到一个填充的效果,把标题栏顶到和绿色区域一致的位置
  2. 标题区,计算出来绿色区域的高度为titleBarHeight,将导航栏标题的高度设置为titleBarHeight,然后用css样式居中对齐。高度一样的情况下,自然就可以对齐一致了。

如下图,红色区域为statusBar的区域,绿色区域为胶囊按钮的区域,胶囊按钮的区域包含灰色的按钮以及上下的padding空白,因此计算整个绿色区域的高度的公式应为:(蓝色线的高度(即胶囊按钮的top的值)- 红色状态栏区域的底statusBarHeight)* 2 + 胶囊按钮的height的值

上面公式是计算出来胶囊按钮区域一个空白乘以2 ,即是俩个空白加上胶囊实体的高度,就是绿色的区域的高度。

代码如下:

<view>
	<view class="fill" :style="{height: navbarHeight + 'px'}"></view>
	<view class="title" :style="{height: titleBarHeight + 'px'}">
	你的AI购物助手~
	</view>
</view>

const navbarHeight = ref(0)
//计算刘海状态栏
navbarHeight.value = uni.getSystemInfoSync().statusBarHeight
//计算胶囊区域的高度
	let {
		top,
		height
	} = uni.getMenuButtonBoundingClientRect()
const titleBarHeight = computed(() => {
	return (top - navbarHeight.value) * 2 + height
})
.fill{
	width:100%;
}
.title{
	width:100%;
	display: flex;
	justify-content: center;
	align-items: center;
}
  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

weixin_39579031

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值