一、前言

  • 产品需求:当用户未授权用户信息的情况下,点击小程序tabbar组件跳出授权信息弹框;若用户已授权用户信息,则点击tabbar组件跳转到相应的地址。
  • 由于小程序原生tabbar组件无法提供支持,所以需要自定义tabbar组件

    二、代码步骤

    2.1 配置信息

  • 在 pages.json中的 tabBar 项指定 custom 字段,同时其余 tabBar 相关配置代码如下:

  1. "tabBar": {
  2. "custom": true,
  3. "color": "#AAAAAA",
  4. "selectedColor": "#C89D66",
  5. "borderStyle": "white",
  6. "backgroundColor": "#ffffff",
  7. "list": [
  8. {
  9. "pagePath": "pages/home/Index",
  10. "iconPath": "static/images/tabBar/icon_home.png",
  11. "selectedIconPath": "static/images/tabBar/icon_home_active.png",
  12. "text": "首页"
  13. },
  14. {
  15. "pagePath": "pages/test/Index",
  16. "iconPath": "static/images/tabBar/icon_test.png",
  17. "selectedIconPath": "static/images/tabBar/icon_test_active.png",
  18. "text": "检测"
  19. },
  20. {
  21. "pagePath": "pages/my/Index",
  22. "iconPath": "static/images/tabBar/icon_my.png",
  23. "selectedIconPath": "static/images/tabBar/icon_my_active.png",
  24. "text": "我的"
  25. }
  26. ]
  27. },

2.2 添加 tabBar 代码文件

在代码根目录下添加入口文件:custom-tab-bar/index.js、custom-tab-bar/index.json、
custom-tab-bar/index.wxml、custom-tab-bar/index.wxss

custom-tab-bar/index.wxml

  1. <view class="tab-bar">
  2. <view class="tab-bar-border"></view>
  3. <view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
  4. <image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
  5. <view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
  6. </view>
  7. </view>

custom-tab-bar/index.js

真机调试和预览模式下wx.switchTab跳转到tabbar的时候会出现返回到上一页再回到 tabbar 的页面,这个现象原生的TabBar也存在,可以使用wx.reLaunch 替代
自定义 tabBar 图片使用base64或本地图片, 防止发生闪烁的现象

  1. Component({
  2. data: {
  3. selected: 0,
  4. color: '#AAAAAA',
  5. selectedColor: '#C89D66',
  6. borderStyle: 'white',
  7. backgroundColor: '#ffffff',
  8. // 自定义 tabBar 图片使用base64或本地图片, 防止发生闪烁的现象
  9. list: [
  10. {
  11. "pagePath": "/pages/home/Index",
  12. "iconPath": "/static/images/tabBar/icon_home.png",
  13. "selectedIconPath": "/static/images/tabBar/icon_home_active.png",
  14. "text": "首页"
  15. },
  16. {
  17. "pagePath": "/pages/test/Index",
  18. "iconPath": "/static/images/tabBar/icon_test.png",
  19. "selectedIconPath": "/static/images/tabBar/icon_test_active.png",
  20. "text": "检测"
  21. },
  22. {
  23. "pagePath": "/pages/my/Index",
  24. "iconPath": "/static/images/tabBar/icon_my.png",
  25. "selectedIconPath": "/static/images/tabBar/icon_my_active.png",
  26. "text": "我的"
  27. }
  28. ],
  29. },
  30. methods: {
  31. // 切换 tab
  32. switchTab(e) {
  33. const data = e.currentTarget.dataset
  34. const url = data.path
  35. // 可在此处进行业务逻辑代码判断
  36. wx.switchTab({url})
  37. },
  38. },
  39. })

custom-tab-bar/index.json

  1. { "component": true }

custom-tab-bar/index.wxss

  1. .tab-bar {
  2. position: fixed;
  3. bottom: 0;
  4. left: 0;
  5. right: 0;
  6. height: 48px;
  7. background: white;
  8. display: flex;
  9. padding-bottom: constant(safe-area-inset-bottom);
  10. padding-bottom: env(safe-area-inset-bottom);
  11. }
  12. .tab-bar-border {
  13. background-color: rgba(0, 0, 0, 0.33);
  14. position: absolute;
  15. left: 0;
  16. top: 0;
  17. width: 100%;
  18. height: 1px;
  19. transform: scaleY(0.5);
  20. }
  21. .tab-bar-item {
  22. flex: 1;
  23. text-align: center;
  24. display: flex;
  25. justify-content: center;
  26. align-items: center;
  27. flex-direction: column;
  28. }
  29. .tab-bar-item image {
  30. width: 52rpx;
  31. height: 52rpx;
  32. }
  33. .tab-bar-item view {
  34. font-size: 10px;
  35. }

2.3 tabBar 组件实例调用

用自定义组件的方式编写即可,该自定义组件完全接管 tabBar 的渲染。另外,自定义组件新增 getTabBar 接口,可获取当前页面下的自定义 tabBar 组件实例

在main.js中添加如下代码:

  1. //使用mixin避免重复书写复制
  2. Vue.mixin({
  3. methods: {
  4. setTabBarIndex(index) {
  5. if (typeof this.$mp.page.getTabBar === 'function' && this.$mp.page.getTabBar()) {
  6. this.$mp.page.getTabBar().setData({
  7. selected: index,
  8. })
  9. }
  10. },
  11. },
  12. })

在页面文件中代码:

  1. onShow() {
  2. this.setTabBarIndex(0) //index为当前tab的索引
  3. }

2.4 组件效果图

小程序自定义tabbar组件详解 - 图1

2.5 安全距离的适配,参考下方相关技术文档

三、参考文档

微信官方自定义tabbar文档
小程序iPhone底部安全距离适配