小程序的自定义tabBar(底部导航栏)

本文档介绍了如何在小程序中实现自定义tabBar。首先,通过在app.json配置custom字段来启用自定义tabBar。其次,创建名为custom-tab-bar的自定义组件,包括index.xml、index.js和index.wxss文件。最后,使用getTabBar()接口获取组件实例并在页面事件中显示底部导航栏,并使用setData()更新页面索引。这是一个实用的小程序开发技巧。

序言

最近做一个小程序的页面需要做底部导航栏,经过研究发现官方文档中给出了两种方法。
第一种可以说也是最简单的,就是自定义tabBar,使用的步骤可以按照官方文档进行。这篇文档就帮你快速写出一个自己的底部导航栏

自定义tabBar

1.配置信息

在app.json中tabBar项指定的custom字段,其余配置可按照文档进行配置

"tabBar": {
    "custom": true,
    "color": "#000000",
    "selectedColor": "#000000",
    "backgroundColor": "#000000",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "pagePath": "pages/index2/index",
        "text": "我的"
      }
    ]
  }
2.添加自定义组件custom-tab-bar

在代码根目录下添加文件,如果使用自定义的tabBar,这里的文件名必须是custom-tab-bar,目录必须是index
index.xml

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

index.js

data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    list: [{
      pagePath: "/pages/index/index",
      iconPath: "/image/icon_component.png",
      selectedIconPath: "/image/icon_component_HL.png",
      text: "首页"
    }, {
        pagePath: "/pages/index2/index",
        iconPath: "/image/icon_API.png",
        selectedIconPath: "/image/icon_API_HL.png",
        text: "我的"
    }]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e){
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({
        url: url,
      })
      this.setData({
        selected:data.index
      })
    }
  }

index.wxss

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

.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;
}

.tab-bar-item cover-image {
  width: 27px;
  height: 27px;
}

.tab-bar-item cover-view {
  font-size: 10px;
}
3.使用getTabBar()接口

写好自定义的组件后,在使用的页面中可以使用getTabBar()接口调用
该接口返回当前页面的 custom-tab-bar 的组件实例

 * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

在页面监听事件里可以使用接口在页面上显示底部导航栏,同时使用setData()为页面的索引赋值,为当前页面添加样式

以上是我在使用小程序自定义的tabBar的使用心得,希望对大家有用!!!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值