UniApp自定义底部TabBar并使用uni.switchTab功能
创作时间:
作者:
@小白创作中心
UniApp自定义底部TabBar并使用uni.switchTab功能
引用
CSDN
1.
https://blog.csdn.net/m0_65629667/article/details/139021324
在UniApp开发中,自定义底部TabBar是一个常见的需求。本文将详细介绍如何在UniApp中实现自定义TabBar,并确保能够使用uni.switchTab功能进行页面切换。
1. 隐藏原生TabBar
首先需要在pages.json
文件中配置原生TabBar的height
属性为0,使其不再显示。同时,需要将自定义TabBar所需的页面添加到配置中。
2. 创建自定义TabBar组件
建议在项目中创建一个components
文件夹,专门存放自定义组件。然后在该文件夹下新建一个TabBar
文件,可以选择使用scss、less或css进行样式编写。
index.vue 文件代码
<template>
<view style="height: 180rpx;">
<view class="content" :style="{zIndex}">
<view class="tabber_box">
<view v-for="(item,index) in tabBars" :key="index"
:style="{width:`${ratio}%`,display:'flex',justifyContent:'space-around',margin: '24rpx 0'}">
<view class="tabber_item" @click="onNav(item.pagePath)">
<image v-if="currentPath === item.pagePath" :src="item.iconPath"></image>
<image v-else :src="item.selectedIconPath"></image>
<text
:style="{color:currentPath === item.pagePath ? item.selectedColor : item.color}">{{item.name}}</text>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
currentPath: { // 当前页面路径
type: String,
default: '/pages/index/index',
},
zIndex: { // 界面层级
type: Number,
default: 10
}
},
data() {
const color = '#FFFFFF66';
const selectedColor = '#FFF';
return {
tabBars: [{
name: '首页',
iconPath: '../../static/tabbar/index.png',
selectedIconPath: '../../static/tabbar/selectd_index.png',
pagePath: '/pages/index/index',
color,
selectedColor,
},
{
name: '邀请',
iconPath: '../../static/tabbar/lnvite.png',
selectedIconPath: '../../static/tabbar/selectd_lnvite.png',
pagePath: '/pages/lnvite/index',
color,
selectedColor,
},
{
name: 'VIP',
iconPath: '../../static/tabbar/vip.png',
selectedIconPath: '../../static/tabbar/selectd_vip.png',
pagePath: '/pages/vip/index',
color,
selectedColor,
},
{
name: '我的',
iconPath: '../../static/tabbar/my.png',
selectedIconPath: '../../static/tabbar/selectd_my.png',
pagePath: '/pages/my/index',
color,
selectedColor,
},
],
ratio: 0,
isLogin: false,
}
},
mounted() {
this.ratio = 100 / this.tabBars.length;
},
methods: {
onNav(url) {
if (this.currentPath !== url) uni.switchTab({
url
});
}
}
}
</script>
index.scss 文件代码
.content {
position: fixed;
bottom: 0;
width: 100%;
.tabber_box {
display: flex;
flex-direction: row;
align-items: center;
background: #151d33;
padding-bottom: calc(env(safe-area-inset-bottom) - 48rpx) ; // 适配iphoneX的底部
padding-bottom: calc(env(safe-area-inset-bottom) - 48rpx); /*兼容 IOS>11.2*/
image {
width: 56rpx;
height: 56rpx;
// margin-bottom: 16rpx;
}
.tabber_item {
display: flex;
flex-direction: column;
align-items: center;
font-size: 28rpx;
}
}
}
3. 引入全局
在main.js
或main.ts
文件中,将创建好的TabBar组件注册到全局中。
4. 使用方法
假设当前页面路径为/pages/index/index
,则在该页面引入并使用自定义TabBar组件,同时将currentPath
参数设置为当前页面路径。其他页面同理。
最终效果
通过以上步骤,你将能够实现一个功能完备的自定义TabBar,并且可以使用uni.switchTab进行页面切换。
热门推荐
中国实现全民免费医疗面临财政、资源分配、医疗质量等挑战
全民免费医疗的可行性研究与建议
我们明明有能力实行免费医疗,但是为什么不实行呢?
阿托伐他汀进口药一片6元,国产药一片2毛多,疗效差别有多大?
灌云豆丹:连云港餐桌上的“绿色瑰宝”
连云港必打卡美食:灌云豆丹和板浦凉粉
燕麦菠菜苹果餐:简单三步打造健康肠道
“尔滨”等地冬季旅游热度爆棚,如何从“网红”到“长红”?
春节里肯定躺着刷过手机,这个姿势有没有?危害比你想象中更大!
跟着小编一起游丽江:白沙古镇,才是真正被低估的宝藏小城
全面解析:如何正确泡养降坡泥以保持其健康与美观?
全面解析:如何正确泡养降坡泥以保持其健与美观?
免费!比丽江古城还好玩,迄今为止我最喜欢的古镇……
芜湖出发:高铁、飞机、长途汽车全方位指南,轻松抵达厦门
《阿凡达2》票房破4亿,揭秘好莱坞品牌化战略新动向
从好莱坞到梦工厂:美国电影工业的百年传奇
元宵节是中国的传统节日吗?元宵节的寓意和象征
探访怀化双璧:洪江古商城与黔阳古城的文化对话
怀化九丰农博园&中坡森林公园:自然美景打卡地
怀化自驾游必去:穿岩山、枫香瑶寨、洪江古商城
《阿凡达2》上映,中国电影如何借鉴好莱坞技术?
《哪吒之魔童闹海》背后的中国电影技术革命
《哪吒2》特效揭秘:中国电影技术崛起之路
阿莫西林、甲硝唑、头孢、沙星...肾毒性大吗?
日本过春节有什么习俗
韩国人如何过年?曾废止农历新年达90年,如今强推“韩式春节”
越南人也过农历新年,但他们的春节风俗却别具一格,快来一起看看吧!
岳阳一日游精华线路:探寻最佳旅游景点与美食
《流浪地球3》筹备进行时:中国科幻电影新高度
小年 “跨南北”,一天之隔,你不知道的那些奇妙差异