uniapp 自定义底部tabbar,支持底部安全区和占位符

本文介绍了如何在Vue项目中使用TabBar组件,并处理底部安全区域的高度计算,以适应iOS设备的布局。
摘要由CSDN通过智能技术生成

 tabbar组件:

<template>
	<view :style="'height: '+placeholder+'rpx'">
		<view class="tabbar-box">
			<view class="tabbar">
				<view class="item" v-for="(item,i) in tabbar" @click="toPage(item.pagePath)">
					<text class="t-icon" :class="i==index?item.seletedIcon:item.icon"></text>
					<view class="title">{{item.text}}</view>
				</view>
			</view>
			<view class="placeholder" :style="'height:'+safeAreaInsetBottom+'rpx;'"></view>
		</view>
	</view>
</template>

<script>
	import util from '../../utils/util';
	export default {
		data() {
			return {
				tabbar: [{
						"pagePath": "/pages/index/index",
						"icon": "t-icon-shouye-weixuanzhongtai",
						"seletedIcon": 't-icon-shouye-xuanzhongtai',
						"text": "首页"
					},
					{
						"pagePath": "/pages/my/my",
						"icon": "t-icon-wode-weixuanzhongtai",
						"seletedIcon": 't-icon-wode-xuanzhongtai',
						"text": "我的"
					}
				],
				//底部安全区域高度
				safeAreaInsetBottom: 0,
				//底部占位符高度
				placeholder: 0
			}
		},
		props: {
			//底部选项卡索引,用于icon高亮
			index: {
				type: String || Number,
				default: 0
			},
		},
		mounted() {
			this.safeAreaInsetBottom = uni.getSystemInfoSync().safeAreaInsets.bottom * 2
			this.$nextTick(() => {
				util.computedElementHeight('.tabbar-box').then(res => {
					this.placeholder = this.safeAreaInsetBottom + res
				})
			})
		},
		methods: {
			toPage(url) {
				uni.switchTab({
					url: url
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.tabbar-box {
		position: fixed;
		width: 100%;
		background: #FFF;
		z-index: 999;
		bottom: 0;

		.tabbar {
			height: 100rpx;
			display: flex;
			justify-content: space-around;
			align-content: center;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				.t-icon {
					width: 50rpx;
					height: 50rpx;
				}

				.title {
					color: #8C8C8C;
					font-size: 20rpx;
				}
			}

		}
	}

	.placeholder {
		background-color: #FFF;
	}
</style>

utils工具类

//动态计算元素高度,返回值单位rpx,调用前一定要加nextTick
const computedElementHeight = async (element) => {
	return new Promise((resolve, reject) => {
		let query = uni.createSelectorQuery();
		query.select(element).boundingClientRect(rect => {
			let clientHeight = rect.height;
			let clientWidth = rect.width;
			let ratio = 750 / clientWidth;
			let height = clientHeight * ratio;
			resolve(height);
		}).exec();
	})
}

export default {
	computedElementHeight
}

 配置:

//pages.json
"tabBar": {
    "custom": true,
	"list": [{
        "pagePath": "pages/index/index"
     },
     {
        "pagePath": "pages/my/my"
     }
    ]
},
//App.vue
onLaunch: function() {
    uni.hideTabBar()
}

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值