微信小程序 自定义 tabBar

自定义 tabBar | 微信开放文档

本文案例使用的Taro 非原生微信小程序

使用流程

1. 配置信息

  • 在 app.json 中的 tabBar 项指定 custom 字段,同时其余 tabBar 相关配置也补充完整。
  • 所有 tab 页的 json 里需声明 usingComponents 项,也可以在 app.json 全局开启。


    案例Taro项目文件:app.config.js:
    export default {
      pages: ['pages/hotel/index', 'pages/order/index', 'pages/room/index', 'pages/mine/index'],
      subPackages: [
        {
          root: 'subPages',
          pages: ['login/index',  'webView/index'],
        },
      ],
      tabBar: {
        custom: true,
        selectedColor: '#da4297',
        backgroundColor: '#fff',
        borderStyle: 'white',
        list: [
          {
            pagePath: 'pages/hotel/index',
            selectedIconPath: 'assets/img/tab/home-fill.png',
            iconPath: 'assets/img/tab/home-line.png',
            text: '首页',
          },
          {
            pagePath: 'pages/order/index',
            selectedIconPath: 'assets/img/tab/order-fill.png',
            iconPath: 'assets/img/tab/order-line.png',
            text: '订单',
          },
          {
            pagePath: 'pages/room/index',
            selectedIconPath: 'assets/img/tab/shop-fill.png',
            iconPath: 'assets/img/tab/shop-line.png',
            text: '商城',
          },
          {
            pagePath: 'pages/mine/index',
            selectedIconPath: 'assets/img/tab/mine-fill.png',
            iconPath: 'assets/img/tab/mine-line.png',
            text: '我的',
          },
        ],
      },
    
    };
    
    tabBar文件类目

2. 添加 tabBar 代码文件

代码根目录下添加入口文件:

custom-tab-bar/index.js
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.wxss

案例:使用的Taro 非原生微信小程序

3. 编写 tabBar 代码

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

index.jsx文件是自定义的tabBar 内容

import { ClassNames, ConstantList, StorageUtil } from '@/util';
import { CoverImage, CoverView } from '@tarojs/components';
import { inject, observer } from 'mobx-react';
import Taro from '@tarojs/taro';
import React, { Component } from 'react';
import './index.scss';

@inject('appStore')
@inject('initMod')
@observer
export default class StoreHome extends Component {
  constructor(props) {
    super(props);
    this.state = {
      color: '#979797',
      selectedColor: '#3D4DA4',
      // 自己定义的字段和内容
      list: [
        {
          pagePath: 'pages/hotel/index',
          selectedIconPath: '/assets/img/tab/home-fill.png',
          iconPath: '/assets/img/tab/home-line.png',
          text: '首页',
        },
        {
          pagePath: 'pages/order/index',
          selectedIconPath: '/assets/img/tab/order-fill.png',
          iconPath: '/assets/img/tab/order-line.png',
          text: '订单',
          requestLogin: true,
        },
        {
          appId: ConstantList.PASS_LOGIN_APPID,
          selectedIconPath: '/assets/img/tab/shop-fill.png',
          iconPath: '/assets/img/tab/shop-line.png',
          text: '商城',
        },
        {
          pagePath: 'pages/mine/index',
          // reallyPath:'/Coupon', 
          selectedIconPath: '/assets/img/tab/mine-fill.png',
          iconPath: '/assets/img/tab/mine-line.png',
          text: '我的',
          requestLogin: true,
        },
      ],
    };
  }

  switchTab = (data, index) => {
    // 如果需要登录 又没有token的话,则跳转到登录页
    const token = Taro.getStorageSync(ConstantList.TOKEN);
    if (data.requestLogin && !token) {
        Taro.navigateTo({
          url: `/subPages/login/index?redirectTo=/${data.pagePath}`,
        });
      return;
    }

    if (!data.building) {
      // 跳转其他小程序
      if (data.appId) {
        Taro.navigateToMiniProgram({
         // xxxxxxxxxxx
        });
      } else if (data.reallyPath) {
         // 跳转H5地址 xxxx
        this.props.initMod.changeSelectedTab(index); // 记录当前高亮tab
      } else {
        Taro.switchTab({ url: '/' + data.pagePath });
        this.props.initMod.changeSelectedTab(index);
      }
    } else {
      Taro.showToast({
        title: '该功能即将开放,敬请期待',
        icon: 'none',
        duration: 1000,
      });
    }
  };

  render() {
    const { list, selectedColor, color } = this.state;
    const { selectedTab } = this.props.initMod;

    return (
      <CoverView
        className={ClassNames('tab-bar', {
          'tab-bar_hide': this.props.appStore.state.hideTarBar
        })}>
        {list.map((item, index) => {
          return (
            <CoverView
              key={'tabBar' + index}
              className='tab-bar-item'
              onClick={() => {
                this.switchTab(item, index);
              }}>
              <CoverView className='tab-bar-box'>
                {item.iconPath && item.selectedIconPath && (
                  <CoverImage
                    className='image imageSelect'
                    src={selectedTab === index ? item.selectedIconPath : item.iconPath}></CoverImage>
                )}
                <CoverView
                  className='text'
                  style={{
                    color: selectedTab === index ? selectedColor : color,
                  }}>
                  {item.text}
                </CoverView>
              </CoverView>
            </CoverView>
          );
        })}
      </CoverView>
    );
  }
}
// 跳转到目标小程序的逻辑
一.通过目标小程序的appid和页面路径跳转
wx.navigateToMiniProgram({
  appId: '目标小程序appid',
  path: '目标小程序页面路径',
  envVersion: 'trial',   // develop开发版;trial体验版;release正式版
  success(res) {
    // 打开成功
    console.log("跳转成功!",res);
  } 
})
二.直接通过目标小程序的页面路径跳转

wx.navigateToMiniProgram({
  shortLink:'目标小程序链接',
  envVersion: 'trial',   // develop开发版;trial体验版;release正式版
  success(res) {
    // 打开成功
    console.log("跳转成功!",res);
  } 
})

index.scss tabBar样式文件


.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: $size-tabbar;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  border-radius: 28px 28px 0 0;
  background: #fff;
  box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.08);
  &_hide {
    display: none;
  }
  .tab-bar-item {
    flex: 1;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    padding: 10px 0;

    .tab-bar-box{
      width: 60px;
      margin: 0 auto;
      position: relative;
      .ring{
        position: absolute;
        display: block;
        width: 16px;
        height: 16px;
        background-color: $color-primary;
        border-radius: 50%;
        right: 0;
        top: 0;
        z-index: 20000;
      }
    }
    .image {
      width: 50px;
      height: 50px;
      margin: 0 auto;

    }
    .text {
      line-height: 30px;
      font-size: 20px;
      white-space: nowrap;
    }
  }
}
 

最终自定义样式效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Embrace924

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值