微信小程序-自定義tabBar

1.定義tabBar

app.json中添加tabBar信息,custom設(shè)為true

"tabBar": {
    "custom": true,
    "backgroundColor": "#f7f7f7",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "邀請函"
      },
      {
        "pagePath": "pages/address/address",
        "text": "地址導(dǎo)航"
      },
      {
        "pagePath": "pages/contact/contact",
        "text": "聯(lián)系我們"
      }
    ]
  },

2.更改tabBar

根目錄下創(chuàng)建custom-tab-bar文件夾
--file index.wxml
這里icon字體圖標(biāo)的做法上一篇介紹過微信小程序-字體圖標(biāo)

<view class="row bg-white justify-between custom-tab-bar" active="{{ active }}" active-color="#07c160">
  <view class="column align-center {{selected === index ? 'color-primary' : 'color-black'}}" 
    wx:for="{{ list }}" wx:key="index" data-index="{{index}}"  bindtap="onChange"  
    data-url="{{item.pagePath}}">
    <icon class="iconfont {{ item.icon }}"></icon>
    {{item.text}}
  </view>
</view>

--file index.js

Component({
  data: {
    selected: 0,
    "list": [
      {
        "pagePath": "/pages/index/index",
        "text": "邀請函",
        "icon": "icon-qingshu"
      },
      {
        "pagePath": "/pages/address/address",
        "icon": "icon-lvhang",
        "text": "地址導(dǎo)航"
      },
      {
        "pagePath": "/pages/contact/contact",
        "icon": "icon-gezi",
        "text": "聯(lián)系我們"
      }
    ]
  },
  methods: {
    onChange(e) {
      const data = e.currentTarget.dataset;
      wx.switchTab({
        url: data.url
      });
    },
    init() {
      const page = getCurrentPages().pop();
      this.setData({
        selected: this.data.list.findIndex(item => item.pagePath === `/${page.route}`)
      });
    }
  }
})

其他頁面需要加入如下

  onShow() {
    this.getTabBar().init();
  }

自定義樣式

.custom-tab-bar {
  height: 50rpx;
  padding: 30px 20px 50px 20px;
  font-family: 'ZCOOL KuaiLe';
  border-top-left-radius: 65% 40%;
  border-top-right-radius: 65% 40%;
}

效果圖

效果圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容