当前位置: 首页 > news >正文

Uniapp之微信小程序自定义底部导航栏形态

发现微信小程序自带导航栏有些丑,只能改变一下常用图标字体颜色什么的,今天就搞个自定义的tab,效果如下。

一,首先就是配置pages.json文件,必须设置"custom": true,  // 关键配置:启用自定义 tabBar

"list" 里面还是和自带的一样,开始研究设置为空[] 但是运行不起来,提示必须得有之前哪些tab信息。

这是我的测试代码

"tabBar" : {"custom": true,  // 关键配置:启用自定义 tabBar"list" : [{"selectedIconPath" : "static/icons/home-active.png","iconPath" : "static/icons/home.png","pagePath" : "pages/index/index","text" : "首页"},{"selectedIconPath" : "static/icons/add-active.png","iconPath" : "static/icons/add.png","pagePath" : "pages/add/index","text" : "记账"},{"selectedIconPath" : "static/icons/analysis-active.png","iconPath" : "static/icons/analysis.png","pagePath" : "pages/analysis/index","text" : "分析"},{ "selectedIconPath" : "static/icons/mine-active.png","iconPath" : "static/icons/mine.png","pagePath" : "pages/mine/index","text" : "我的"}]}

二、设置 公用 tab 导航页面,新建 components--------custom-tabbar目录 及custom-tabbar.vue文件页面

custom-tabbar.vue 

<template><view class="custom-tabbar"><view class="tabbar-item" v-for="(item, index) in tabList" :key="index"@click="switchTab(item.path)"><!-- 图标 --><view class="icon-wrapper"><image :src="currentPath === item.path ? item.selectedIcon : item.icon" mode="widthFix"class="tabbar-icon"></image></view><!-- 文字 --><text class="tabbar-text":class="{ 'active': currentPath === item.path }">{{ item.text }}</text></view></view>
</template><script>
export default {data() {return {currentPath: '',  // 当前页面路径// 导航栏配置tabList: [{path: '/pages/index/index',text: '首页',icon: '/static/icons/home.png',selectedIcon: '/static/icons/home-active.png'},{path: '/pages/add/index',text: '记账',icon: '/static/icons/add.png',selectedIcon: '/static/icons/add-active.png'},{path: '/pages/analysis/index',text: '分析',icon: '/static/icons/analysis.png',selectedIcon: '/static/icons/analysis-active.png'},{path: '/pages/mine/index',text: '我的',icon: '/static/icons/mine.png',selectedIcon: '/static/icons/mine-active.png'}]}},
created() {// 监听全局路由变化this.routeListener = uni.onAppRoute((res) => {// 路由变化后,延迟获取页面栈(确保页面已切换完成)setTimeout(() => {const pages = getCurrentPages()this.currentPath = "/" + pages[pages.length - 1].routeconsole.log('路由变化,更新路径:', this.currentPath)}, 100)})},beforeDestroy() {// 销毁时移除监听,避免内存泄漏this.routeListener()},methods: {// 切换页面switchTab(path) {// 如果点击的是当前页面,不做操作if (this.currentPath === path) return// 使用 switchTab 跳转(适合 tab 页面)uni.switchTab({url: path})}}
}
</script><style scoped>
.custom-tabbar {display: flex;justify-content: space-around;align-items: center;position: fixed;bottom: 3%;left: 2%;right: 2%;height: 50px;border-radius: 25px;box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);z-index: 999;background: #fff;
}.tabbar-item {display: flex;flex-direction: column;align-items: center;justify-content: center;flex: 1;
}.icon-wrapper {height: 24px;margin-bottom: 2px;
}.tabbar-icon {width: 24px;height: 24px;
}.tabbar-text {font-size: 12px;color: #2c2c2c;
}.tabbar-text.active {color: #007aff;  /* 选中状态颜色 */
}
</style>

三、引用到所需要的页面即可,举例:index.vue

1,页面部分

<template><view class="container"><!-- 页面内容 --><custom-tabbar></custom-tabbar></view>
</template>

2,js 部分:设置引入页面路径,

<script>
import customTabbar from '@/components/custom-tabbar/custom-tabbar.vue'
export default {components: {customTabbar} 
}
</script>

3,css 部分

.container {padding-bottom: 50px;/* 适配安全区 */padding-bottom: calc(50px + constant(safe-area-inset-bottom));padding-bottom: calc(50px + env(safe-area-inset-bottom));
}

基本完事,可以试试了。

http://www.dtcms.com/a/332341.html

相关文章:

  • 北京JAVA基础面试30天打卡10
  • 数据资产运营——解读 167页 2025 县域数据资产运营蓝皮书【附全文阅读】
  • 5G工业一体机汽车零部件工厂的无纸化管理
  • [激光原理与应用-285]:理论 - 波动光学 - 无线电磁波的频谱分配
  • [激光原理与应用-286]:理论 - 波动光学 - 不同频段电磁波的特点与差异性
  • 局部变量与全局变量的关系及应用
  • 46.Sentinel规则持久化
  • FreeRTOS中断服务程序(ISR)详细讲解
  • 从ChatGPT到智能助手:Agent智能体如何颠覆AI应用
  • 基于uiautomation的自动化流程RPA开源开发演示
  • 机器学习——PCA(主成分分析)降维
  • 开源 Arkts 鸿蒙应用 开发(十五)自定义绘图控件--仪表盘
  • STM32 - Embedded IDE - GCC - 解决LWRB库在GCC编译器会编译失败,在ARMCC编译器时却正常编译
  • 【GUI】ssh实现gui本地可视
  • 公司的服务器怎么个事,服务器是什么东西
  • 系统思考:情绪内耗与思维模式
  • 开源长期记忆 短期记忆 框架调研对比19999字
  • 4.4 vue3生命周期函数
  • 解决在uniapp真机运行上i18n变量获取不到问题
  • Vue2与Vue3生命周期函数全面解析:从入门到精通
  • 【测试用例】
  • Qt 常用控件 - 9
  • 小兔鲜儿-小程序uni-app(二)
  • 手机端的音视频界面或者图片文档界面共享给大屏
  • 从源码到可执行文件:hello.c 的二进制之旅
  • Java项目基本流程(四)
  • 基于阿里云音频识别模型的网页语音识别系统实现
  • 人工智能与社会治理:从工具到生态的范式重构
  • spring中异步任务注解@Async和@scheduled的使用
  • Redis核心应用场景及代码案例