小程序自定义导航栏(兼容iphoneX)

第一步,首先在pager.json中修改globalStyle中的navigationStyle属性(default是默认哟)

 第二步,在APP.vue中获取自定义tabBar高度

export default{
    globalData: {
			statusBarHeight:0,
			titleBarHeight:0
		},
    onLaunch:function(){
        this.getStatusBar()
    },
    methods:{
		getStatusBar(){
		uni.getSystemInfo({
			success: (res) => {
			this.globalData.statusBarHeight = res.statusBarHeight
			this.globalData.titleBarHeight = uni.getMenuButtonBoundingClientRect().bottom + uni.getMenuButtonBoundingClientRect().top - (res.statusBarHeight * 2)
				  }
				})
			},
			// 获取自定义tabbar的高度,rpx单位
			getTabBarH(){
				return this.unitTransform(getApp().globalData.statusBarHeight + getApp().globalData.titleBarHeight,'rpx')
			},
			// 获取tabbar高度-兼容不同设备,兼容iphoneX,返回 50px 字符串
			getTabbarH(){
				const res = uni.getSystemInfoSync();
				let tabBarH = (res.screenHeight-res.safeArea.bottom + 50)+'px'
				return tabBarH
			},
			// 单位转换 px 转 rpx  type转换类型
			unitTransform(num,type){
				switch(type){
					case 'rpx': return num * 750 / uni.getSystemInfoSync().windowWidth;break;
					case 'px': return num / 750 * wx.getSystemInfoSync().windowWidth;break;
				}
			}
		}
}

第三步,运用到页面中,再设置一下样式就可以啦

<template>
	<view class="">
		<!-- 导航栏 -->
		<view class="w-100" style="background: pink;">
			<view :style="{height:statusBarHeight+'px'}"></view>
			<view :style="{height:titleBarHeight+'px'}" class="f32 flex ja">我的</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBarHeight: 0,
				titleBarHeight: 0
			}
		},
		onLoad() {
			this.statusBarHeight = getApp().globalData.statusBarHeight
			this.titleBarHeight = getApp().globalData.titleBarHeight
		}
	}
</script>

<style>
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值