小程序官方tabbar和自定义tabbar

2023-10-27

uniapp官方tabbar:

  1. 打开项目中的 pages.json 文件。

  2. 在 JSON 对象中添加一个名为 tabBar 的字段,并设置其值为一个对象。

  3. tabBar 对象中,配置 colorselectedColor 字段来定义 TabBar 的默认颜色和选中项的颜色。示例:

     

    "tabBar": {
      "color": "#999999",  //默认颜色
      "selectedColor": "#FF0000" //选中项的颜色
    }
    

  4. 添加一个名为 list 的数组字段,用于定义 TabBar 的每个选项。

  5. list 数组中,每个对象表示一个选项,可以设置 pagePathtexticonPathselectedIconPath 字段来定义选项的路径、文本、默认图标和选中图标。示例:
     

    "tabBar": {
      "color": "#999999", //tab 上的文字默认颜色
      "selectedColor": "#FF0000", //tab 上的文字选中时的颜色
      "borderStyle": "black", //bat上边框颜色
      "backgroundColor": "#fff", //tab的背景色
      "list": [
        {
          "pagePath": "pages/index/index",  //页面路径,必须在 pages 中先定义
          "text": "首页",                    //tab 上按钮文字
          "iconPath": "static/tabbar/home.png", //默认图片路径
          "selectedIconPath": "static/tabbar/home-selected.png" //选中时的图片路径
        },
        {
          "pagePath": "pages/cart/cart",//页面路径,必须在 pages 中先定义
          "text": "购物车",             //tab 上按钮文字
          "iconPath": "static/tabbar/cart.png", //默认图片路径
          "selectedIconPath": "static/tabbar/cart-selected.png" //选中时的图片路径
        },
        {
          "pagePath": "pages/profile/profile", //页面路径,必须在 pages 中先定义
          "text": "个人中心", //tab 上按钮文字
          "iconPath": "static/tabbar/profile.png",//默认图片路径
          "selectedIconPath": "static/tabbar/profile-selected.png" //选中时的图片路径
        }
      ]
    }
    

    配置完毕后,保存 pages.json 文件

uniapp自定义tabbar组件

pages.json 文件中配置自定义 TabBar。在tabbar中加上"custom": true,就变成了自定义tabbar。代码如下:

"tabBar": {
  "custom": true,     //改为自定义tabbar 
  "color": "#999999", //tab 上的文字默认颜色
  "selectedColor": "#FF0000", //tab 上的文字选中时的颜色
  "borderStyle": "black", //bat上边框颜色
  "backgroundColor": "#fff", //tab的背景色
  "list": [
    {
      "pagePath": "pages/index/index",  //页面路径,必须在 pages 中先定义
      "text": "首页",                    //tab 上按钮文字
      "iconPath": "static/tabbar/home.png", //默认图片路径
      "selectedIconPath": "static/tabbar/home-selected.png" //选中时的图片路径
    },
    {
      "pagePath": "pages/cart/cart",//页面路径,必须在 pages 中先定义
      "text": "购物车",             //tab 上按钮文字
      "iconPath": "static/tabbar/cart.png", //默认图片路径
      "selectedIconPath": "static/tabbar/cart-selected.png" //选中时的图片路径
    },
    {
      "pagePath": "pages/profile/profile", //页面路径,必须在 pages 中先定义
      "text": "个人中心", //tab 上按钮文字
      "iconPath": "static/tabbar/profile.png",//默认图片路径
      "selectedIconPath": "static/tabbar/profile-selected.png" //选中时的图片路径
    }
  ]
}

然后在下边加上usingComponents字段

"tabBar": {
  "custom": true,     //改为自定义tabbar 
  "color": "#999999", //tab 上的文字默认颜色
  "selectedColor": "#FF0000", //tab 上的文字选中时的颜色
  "borderStyle": "black", //bat上边框颜色
  "backgroundColor": "#fff", //tab的背景色
  "list": [
    {
      "pagePath": "pages/index/index",  //页面路径,必须在 pages 中先定义
      "text": "首页",                    //tab 上按钮文字
      "iconPath": "static/tabbar/home.png", //默认图片路径
      "selectedIconPath": "static/tabbar/home-selected.png" //选中时的图片路径
    },
    {
      "pagePath": "pages/cart/cart",//页面路径,必须在 pages 中先定义
      "text": "购物车",             //tab 上按钮文字
      "iconPath": "static/tabbar/cart.png", //默认图片路径
      "selectedIconPath": "static/tabbar/cart-selected.png" //选中时的图片路径
    },
    {
      "pagePath": "pages/profile/profile", //页面路径,必须在 pages 中先定义
      "text": "个人中心", //tab 上按钮文字
      "iconPath": "static/tabbar/profile.png",//默认图片路径
      "selectedIconPath": "static/tabbar/profile-selected.png" //选中时的图片路径
    }
  ]
},
 //usingComponents字段是在页面的 JSON 配置文件(如 pages.json)中使用的,用于声明并引入自定义组件。

  "usingComponents": {
	    "customtabbar": "custom-tab-bar/index"。//引入自定义组件,键为组件名,值为组件路径
	  },

这样代码写完就是在pages.json里配置完成了,然后封装一个组件名字为custom-tab-bar的tabbar文件

文件里包含四个文件复制即可如下:

 

index.js:

Component({
	data: {
		selected: null,
		color: '#78747F',
		selectedColor: '#007AFF',
		list: [{
			pagePath: '/tabbar/home/index',
			iconPath: '/static/icons/img/home.png',
			selectedIconPath: '/static/icons/img/home2.png',
			text: '首页'
		}, {
			pagePath: '/tabbar/me/index',
			iconPath: '/static/icons/img/me.png',
			selectedIconPath: '/static/icons/img/me2.png',
			text: '我的'
		}]
	},
	attached() { },
	methods: {
		switchTab(e) {
			const data = e.currentTarget.dataset
			const url = data.path
			console.log(data);
			console.log(url);
				wx.switchTab({
					url  //就是要跳转的tabbar页面
				})
		},

	}
})

index.json:

{
  "component": true
}

index.wxml:这个是tabbar html页面 list是tabbar里定义的对象

<!--miniprogram/custom-tab-bar/index.wxml-->
<view class="tab-bar">
  <view class="tab-bar-border"></view>
  <view wx:for="{{list}}" wx:key="index" class="{{!item.text ? 'big' : ''}} tab-bar-item flex-1 item{{index}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
	<image wx-if="{{selected === index}}"  src="{{item.selectedIconPath}}" mode="aspectFit"></image>
	<image wx-if="{{selected !== index}}"  src="{{item.iconPath}}" mode="aspectFit"></image>
    <view class="btn-box" wx-if="{{item.text}}" style="color: {{selected === index ? selectedColor : color}}">
		{{item.text}}
	<!-- 	<button wx-if="{{index === 4}}" class="btn" type="default" open-type="getUserInfo" bindgetuserinfo="auth"></button> -->
	</view>
  </view>
</view>	


index.wxss:

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height:55px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  z-index:-1;
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-size: 13px;
}
.tab-bar-item image {
  width:36.23rpx;
  height:48rpx;
}

/* .tab-bar-item-image-0 {
  width:36.23rpx;
  height:48rpx;
} */

/* .tab-bar-item-image-1 { 
  width:100rpx;
  height:48rpx !important;
} */

.tab-bar-item.big image{
	width: 61.59rpx;
	height: 61.59rpx;
}
.tab-bar-item cover-image {
  width: 48.91rpx;
  height: 48.91rpx;
}

.tab-bar-item cover-view {
  font-size: 10px;
}
.tab-bar-item  {
	position: relative;
}
.tab-bar-item .btn-box .btn {
	opacity: 0;
	padding: 0;
	height: 50px;
	width: 100%;
	position: absolute;
	left: 0;
	top: 0;
}

 封装完以后在每个tabbar页面中加一下代码:

 

home页面
onShow() {
			const page = this.$mp.page
			console.log(page.getTabBar(),'page');
			if (typeof page.getTabBar === 'function' && page.getTabBar()) {
				page.getTabBar().setData({
					selected: 0 这个下标就是封装tabbar中list数组中的数据下标
				})
			}
		
		},


//page 是通过 this.$mp.page 获取的当前页面实例
//typeof page.getTabBar === 'function' 是用于检查 page.getTabBar 是否为一个函数,以确保可以调用 getTabBar 方法。
//page.getTabBar() 调用 getTabBar 方法,返回当前页面所属的 TabBar 实例。
//page.getTabBar().setData({ selected: 0 }) 调用 TabBar 实例的 setData 方法,将选中项的索引设置为 0。这样就会使 TabBar 中索引为 0 的项高亮显示,表示当前选中的项。
me页面
onShow() {
			const page = this.$mp.page
			console.log(page.getTabBar(),'page');
			if (typeof page.getTabBar === 'function' && page.getTabBar()) {
				page.getTabBar().setData({
					selected: 1 //这个下标就是封装tabbar中list数组中的数据下标
				})
			}
		
		},


//page 是通过 this.$mp.page 获取的当前页面实例
//typeof page.getTabBar === 'function' 是用于检查 page.getTabBar 是否为一个函数,以确保可以调用 getTabBar 方法。
//page.getTabBar() 调用 getTabBar 方法,返回当前页面所属的 TabBar 实例。
//page.getTabBar().setData({ selected: 1 }) 调用 TabBar 实例的 setData 方法,将选中项的索引设置为 1。这样就会使 TabBar 中索引为 1 的项高亮显示,表示当前选中的项。

 这样自定义tabbar就好了!!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

小程序官方tabbar和自定义tabbar 的相关文章

  • python程序员 培训

    非常感谢大家这么长时间对我们的喜爱和关注 我们都知道 python是当前的大趋势 无论是就业前景 发展空间 还是薪资待遇都是当下最为火爆的行业 所以我们特意联系了央视公开课曾推荐的万门大学 向大家赠送一份人工智能的课程 这个课程我们已经了解
  • QSlider风格设置

    QT的滑动条在开发的过程中经常使用 默认的QSlider风格比较简陋 一般达不到UI设计的效果 本篇记录一个QSlider使用过程中风格的设置 1 qss常用的字段属性 1 1背景属性 属性 值 意思 background Backgrou

随机推荐

  • 用C语言实现经典游戏——贪吃蛇

    目录 1 游戏实现思想 1 定义蛇对象 2 食物对象 3 分数 4 初始化蛇 5 初始化食物 6 修改控制台光标位置 7 画出蛇和食物 8 蛇的移动控制 9 开始游戏 10 蛇移动 11 画墙 12 去除蛇尾 13 去除光标 14 显示分数
  • 06.数据库日常应用实例

    生活事例 01 用户注册 A select操作 搜索数据库所有用户名 B 与新注册的用户名进行比对 C 如果相同 新用户需从新改名 D 直到与数据库中的用户名不同 方能注册 02 转账 A select操作 搜索数据库所有账号 B 收款方账
  • Please enable Javascript to view this page

    今天进防火墙时出错 以下设置解决 internet选项 gt 安全 gt 受信任站点 gt 添加站点 转载于 https www cnblogs com cw828 p 10063200 html
  • 测试原则-阶段-测试用例设计-调试

    测试原则 阶段 测试用例设计 调试 测试原则和方法 测试阶段 测试策略 测试用例的设计 调试 软件度量 测试原则和方法 系统测试 成功的测试 测试原则 软件测试的方法 静态测试 动态测试 测试阶段 单元测试 集成测试 确认测试 系统测试 配
  • 【ETH链游】阿蟹Axie Infinity模拟器运行及多开

    Axie Infinity 众所周知 阿蟹 Axie Infinity 是去年最火的一款GameFi游戏 由越南团队Sky Mavis开发 短短几个月内就红遍东南亚 最大市场在菲律宾 其次是越南 马来西亚 印尼与美国等 去年6月 该游戏单日
  • 个人学习经验: C++ ifndef 作用和用法

    ifndef是if not define的简写 一般的使用场景为 1 头文件中使用 防止头文件被多重调用 2 作为测试使用 省去注释代码的麻烦 3 作为不同角色或者场景的判断使用 头件的中的 ifndef 这是一个很关键的东西 比如你有两个
  • react函数式组件传值之子传父

    在用react进行函数式编程时 父组件可以通过props向子组件传值 那么子组件怎么向父组件传值呢 首先 父组件需要向子组件传递一个函数 然后 子组件通过props获取函数并附上参数 最后 父组件通过函数拿到子组件传递的值 一 具体案例 父
  • gridcontrol选中多行数据进行复制_奇技淫巧:在 ssh 里面把服务器的文本复制到本地电脑...

    点击上方 Python编程时光 选择 加为星标 第一时间关注Python技术干货 2020年 力度最大的购书优惠 千万别错过了 使用 macOS 的同学 应该熟悉一个命令pbcopy 它可以在命令行中把一段内容写入到剪贴板 例如 echo
  • Java安装配置教程,2023年最新版,全部版本看这一篇就够了!!

    JDK新手无脑安装配置教程 JDK下载网址 ps 如果你的JDK版本在官网没有找到 可以通过第三方资源进行下载 下载安装配置教程是通用的 官方链接 gt https www oracle com java technologies java
  • 关于ViewGroup$ViewLocationHolder$mRoot的内存泄漏

    今儿遇到个场景 在Android P API 28 中 在退出了含有RecyclerView的RelativeLayout中 LeakCanary报了这么一个内存泄漏 1 定位问题 1 1 定位源码 在AndroidP中ViewGroup内
  • 初探计算机网络代理

    初探计算机网络代理 文章目录 初探计算机网络代理 什么是计算机网络代理 代理的类型有哪些 正向代理 反向代理 正向代理的实现原理是什么 普通代理 隧道代理 SOCKS 协议 反向代理的实现原理是什么 都有哪些代理产品 TL DR 这篇文章介
  • CH8-多线程

    案例8 1 龟兔赛跑 案例介绍 1 任务描述 众所周知的 龟兔赛跑 故事 兔子因为太过自信 比赛中途休息而导致乌龟赢得了比赛 本案例要求编写一个程序模拟龟兔赛跑 乌龟的速度为1米 1500毫秒 兔子的速度为5米 500毫秒 等兔子跑到第70
  • Prometheus在kubernetes集群的搭建教程

    Prometheus在kubernetes集群的搭建 一 Prometheus介绍 1 Prometheus简介 2 Prometheus介绍 3 Prometheus与市面的监控系统区别 4 Prometheus特点 5 Promethe
  • 21.QT-QTreeWidget,QTabWidget

    QTreeWidget树形列表 设置标签相关函数 void QTreeWidget setHeaderItem QTreeWidgetItem item void QTreeWidget setHeaderLabel constQStrin
  • 论文笔记:NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis

    目录 文章摘要 1 Neural Radiance Field Scene Representation 基于神经辐射场的场景表示 2 Volume Rendering with Radiance Fields 基于辐射场的体素渲染 2 1
  • 奇异值分解 (SVD)原理及python实现

    奇异值分解 Singular Value Decomposition SVD 是一种矩阵分解 Matrix Decomposition 的方法 除此之外 矩阵分解还有很多方法 例如特征分解 Eigendecomposition LU分解 L
  • Linux Kernel编译流程 (二)

    1 vmlinux 研究vmlinux文件的产生 zImage和Image产生 Linux Kernel 4 18 20 Source Insight 3 5 Ubuntu 18 04 arm linux gnueabi xxx 1 1 f
  • 油盐微服务——服务容错保护Hystrix

    文章目录 引入Hystrix 由于网络问题或者依赖服务自身的问题出现的调用故障或者延迟 如果此时调用方请求不断增加 就会形成任务积压 最终导致自身服务的瘫痪 比如在一个电商网站中 可能会讲系统拆分成为用户 订单 库存 积分 评论等一系列服务
  • svn 撤销已经add的文件

    在svn add了某文件之后 发现某个文件不需要被修改 这时候可以使用revert命令来撤销add操作 svn revert 文件路径 如果需要将之前add的内容都撤销掉的话 一个文件或者一个文件夹来revert就会有点慢 这时候直接使用r
  • 小程序官方tabbar和自定义tabbar

    uniapp官方tabbar 打开项目中的 pages json 文件 在 JSON 对象中添加一个名为 tabBar 的字段 并设置其值为一个对象 在 tabBar 对象中 配置 color 和 selectedColor 字段来定义 T