微信小程序-自定义tabBar

1.定义tabBar

app.json中添加tabBar信息,custom设为true

"tabBar": {
    "custom": true,
    "backgroundColor": "#f7f7f7",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "邀请函"
      },
      {
        "pagePath": "pages/address/address",
        "text": "地址导航"
      },
      {
        "pagePath": "pages/contact/contact",
        "text": "联系我们"
      }
    ]
  },

2.更改tabBar

根目录下创建custom-tab-bar文件夹
--file index.wxml
这里icon字体图标的做法上一篇介绍过微信小程序-字体图标

<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": "地址导航"
      },
      {
        "pagePath": "/pages/contact/contact",
        "icon": "icon-gezi",
        "text": "联系我们"
      }
    ]
  },
  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%;
}

效果图

效果图

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容