微信小程序自定义导航栏(兼容适配所有机型)

在开发微信小程序的过程中,我们发现只是官方默认的header头部,满足不了设计需求,这里我们以Uni App为例讲解一下。

官方文档:navigationStyle 导航栏样式,仅支持 default/custom。custom 模式可自定义导航栏,只保留右上角胶囊状的按钮)。
文档地址: https://uniapp.dcloud.io/collocation/pages?id=pages
在这里插入图片描述

1、不同的机型,状态栏的高度是不一样的
uni.getSystemInfo({
    success:function(e){
		Vue.prototype.windowHeight = e.windowHeight *2
		Vue.prototype.statusBar    = e.statusBarHeight *2
		Vue.prototype.customBar    = 88
		if(e.platform == "devtools"){
		     Vue.prototype.platformType ='devtools'
		}else if(e.platform == "ios"){
             Vue.prototype.platformType ='ios'
		}else if(e.platform == "android"){
             Vue.prototype.platformType ='android'
		}
    }
})
2、根据状态栏高度,编写header组件
<template>
    <view class="hy-body-wrap" :style="{'padding-top':style}">
		<image v-if="baseUrl && backImg" class="back-box" :src="baseUrl+'user/back-user.png'" :style="{'height':style}"></image>
		<view class="c-head-wrap" :class="[{'g-border':borderAsync},{'back':!backImg}]" :style="{'padding-top': statusBarH + 'rpx'}">
			<view class="head-flex-box" :style="[{'height': customBarH + 'rpx', 'color': titleTintColor}, bgColor]">
			    <!-- 返回 -->
			    <view v-if="isBack" @tap="goBack" class="go-back-btn g-cen-y">
						
			    </view>
				<view v-else class="right-back"></view>
			    <!-- <slot name="headerL"></slot> -->
			    <!-- 标题 -->
			    <view :class="[{'text-left':platformType !='ios'},{'ff':backImg}]" class="head-title flex1 g-cen-cen" v-if="title">
			        {{title}}
			    </view>
			    <view class="flex1" :class="[searchRadius ? 'uni_searchRadius' : '']" v-if="search"> />
			        <input class=" flex1" type="text" placeholder="搜索" placeholder-style="color: rgba(255,255,255,.5);" />
			    </view>
			    <!-- 右侧 -->
			    <view class="right-back">
			        <slot name="iconfont"></slot>
			        <slot name="string"></slot>
			        <slot name="image"></slot>
			    </view>
			</view>
		</view>
        
		<slot name="body"></slot>
		<slot name="nav"></slot>
		<slot name="search"></slot>
    </view>
</template>
 
<script>
import {mapActions,mapGetters} from 'vuex'
export default {
	data() {
		return {
			statusBarH: this.statusBar,
			customBarH: this.customBar
		}
	},
	props: {
		isBack: { type: [Boolean, String], default: true },
		title: { type: String, default: '' },
		titleTintColor: { type: String, default: '#343434' },
		bgColor: Object,
		center: { type: [Boolean, String], default: false },
		search: { type: [Boolean, String], default: false },
		searchRadius: { type: [Boolean, String], default: false },
		fixed: { type: [Boolean, String], default: false },
		hei:{type:Number,default:0},
		borderAsync:{type:Boolean,default:false},
		backImg:{type:Boolean,default:false}
	},
	computed: {
		style() {
			let hei = `${this.customBarH+this.statusBarH+this.hei}rpx`;
			let _style = hei;
			this.setStatusBarH(this.customBarH+this.statusBarH+'rpx');
			return _style
		},
		...mapGetters(['baseUrl'])
		
	},
	methods: {
		...mapActions(['setStatusBarH']),
		goBack() {
			uni.navigateBack()
		}
	}
}
</script>
<style lang="scss" scoped>
.hy-body-wrap{
	.back-box{
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		z-index: 12;
	}
	.c-head-wrap{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 100;
		&.back{
			background-color: #fff;
		}
		.head-flex-box{
			display: flex;
			align-items: center;
			position: relative;
			.flex1{
				flex:1;
			}
			.head-title{
				font-size: 32px;
				&.ff{
					color: #fff;
				}
				&.text-left{
					justify-content: flex-start;
				}
			}
			.go-back-btn{
				padding-left: 31px;
				height: 100%;
				width:80px ;
				box-sizing: border-box;
				&::after{
					content: "";
					width: 14px;
					height: 28px;
					background: url($baseUrl+'icon/jiao-left.png') no-repeat center;
					background-size: cover;
				}
			}
			.right-back{
				width: 30px;
				height: 100%;
			}
			
		}
	}
}
</style>
3、引用方式
<template>
	<section class="work-wrap" >
		<header-box :isBack="false" title="这时title内容" />
	</section>
</template>
<script>
import api from "$api"
import headerBox from '$common/header/headerBox'
export default {
	data() {
		return {
			
		};
	},
	components:{
		headerBox
	}
}
</script>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现微信小程序自定义navigationbar顶部导航栏兼容适配,首先需要明确几个关键点。 1. 自定义导航栏的设计:根据产品需求和界面设计,确定导航栏的样式、背景色、标题和按钮等元素。 2. 获取手机屏幕的尺寸:小程序可以使用`wx.getSystemInfoSync()`方法获取手机屏幕的尺寸信息。 3. 设置导航栏高度:根据系统的不同,导航栏的高度也会有所差异。可以把导航栏的高度设置为一个相对较大的值,以适应大部分机型。当实际机型不同导航栏高度时,可以根据不同机型进行适配。 4. 适配不同机型的刘海屏或异形屏:对于刘海屏或者异形屏的机型,需要判断屏幕顶部是否存在刘海等异形部分,并进行相应的适配。可以使用`wx.getMenuButtonBoundingClientRect()`方法来获取右上角胶囊按钮的位置信息,从而确定导航栏背景的偏移和大小。 5. 设置页面的顶部内边距:在有自定义导航栏的情况下,为了避免导航栏遮挡页面内容,需要设置页面的顶部内边距。 6. 适配多端:微信小程序支持在不同的平台上运行,如iOS、Android、小程序开发工具等。因此要确保自定义导航栏在不同平台上的显示效果一致。 综上所述,通过了解手机屏幕尺寸、设置导航栏高度和背景、适配刘海屏、设置页面内边距以及多端适配等步骤,可以实现微信小程序自定义navigationbar顶部导航栏兼容适配,使其在不同机型上都能够正常显示和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值