微信小程序自定义tabBar(超详细简单以及tabBar选中效果不改变解决办法)

我们可以先去了解一下官方文档: https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

老规矩先看一下效果图:

项目结构:

custom-tab-bar/index.wxml

<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>

custom-tab-bar/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;
}

custom-tab-bar/index.js

Component({
  data: {
    selected: 0,
    color: "#999999",
    selectedColor: "#F7393F",
    list: [{
      pagePath: "/pages/index/index",
      iconPath: "/img/base/icon-home.png",
      selectedIconPath: "/img/base/icon-home-red.png",
      text: "首页"
    }, {
        pagePath: "/pages/my/my-chat/index",
        iconPath: "/img/base/icon-chat.png",
        selectedIconPath: "/img/base/icon-chat-red.png",
      text: "聊天"
      }, {
        pagePath: "/pages/live-streaming/index",
        iconPath: "/img/base/icon-live-streaming.png",
        selectedIconPath: "/img/base/icon-live-streaming-red.png",
        text: "直播"
      }, {
        pagePath: "/pages/my/my-shopping-trolley/index",
        iconPath: "/img/base/icon-shopping-trolley.png",
        selectedIconPath: "/img/base/icon-shopping-trolley-red.png",
        text: "购物车"
      }, {
        pagePath: "/pages/my/my-personal-center/index",
        iconPath: "/img/base/icon-user.png",
        selectedIconPath: "/img/base/icon-user-red.png",
        text: "个人中心"
      }]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
      this.setData({
        selected: data.pages
      })
    }
  }
})

custom-tab-bar/index.json

{
  "component": true
}

以上就是所以自定义tabBar代码

接下来看看怎么引入:

全局配置app.json

"tabBar": {
    "custom": true,
    "color": "#999999",
    "selectedColor": "#F7393F",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": "pages/index/index",
        "iconPath": "img/base/icon-home.png",
        "selectedIconPath": "img/base/icon-home-red.png",
        "text": "首页"
      },
      {
        "pagePath": "pages/my/my-chat/index",
        "iconPath": "img/base/icon-chat.png",
        "selectedIconPath": "img/base/icon-chat-red.png",
        "text": "聊天"
      },
      {
        "pagePath": "pages/live-streaming/index",
        "iconPath": "img/base/icon-live-streaming.png",
        "selectedIconPath": "img/base/icon-live-streaming-red.png",
        "text": "直播"
      },
      {
        "pagePath": "pages/my/my-shopping-trolley/index",
        "iconPath": "img/base/icon-shopping-trolley.png",
        "selectedIconPath": "img/base/icon-shopping-trolley-red.png",
        "text": "购物车"
      },
      {
        "pagePath": "pages/my/my-personal-center/index",
        "iconPath": "img/base/icon-user.png",
        "selectedIconPath": "img/base/icon-user-red.png",
        "text": "个人中心"
      }
    ]
  },

主要一行代码"custom": true,

点选选中效果不改变或者点击两次才改变解决办法:

在每个页面js中onShow方法里面加入

onShow: function() {

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0  //这个数字是当前页面在tabBar中list数组的索引
      })
    }
}

问题就解决了

 

如果对你有帮助,请关注一下博主的小程序支持一下, 在此谢谢了

 
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值