首页 > 其他分享 >Taro自定义Tabbar

Taro自定义Tabbar

时间:2022-10-17 13:46:42浏览次数:47  
标签:Taro tab .. pagePath Tabbar tabbar resource png 自定义

 

在项目中遇到需要自定义底部的tabbar,首先需要了解底部tabbar最少2个最多5个,项目中可能会遇到自定义时底部的tabbar总个数超过5个的情况,

比如:在商家端小程序底部需要显示3个tabbar,客户端小程序也需要显示3个tabbar,此时商家端和客户端的tabbar都不相同,此时自定义就可以用到。

方法一: 就是根据app.config.js来设置自定义的tabbar,好处就是不用在页面中引入就可以使用

步骤一:在app.config.js中增加自定义tabbar开关custom:true

 步骤二:  在src目录下新建custom-tab-bar组件

大部分的内容,不可直接复制粘贴使用,代码进行了简单的替换,注意文件名称 customTabBar 首字母要小写,如果是大写是自定义组件写法,如果项目使用了redux,建议将TabList数据放在redux中

/**
 * 页面底部导航栏下标
 * */
export const PageTabBarEnum = {

  TabHomePage: 1,    // 自有小程序首页
  TabOrderDishesPage: 2,  // 点餐首页
  TabMemberPage: 3,   // 会员中心页
  TabPdwHomePage: 4,   // 平台小程序首页
  TabPdwOrderListPage: 5,  // 平台订单列表页
  TabPdwMemberPage: 6   // 平台小程序会员
}

// 外卖平台
const PlatformEnum = {
  Merchant: 0,
  Pdw: 1,
}

const TabList = [
  {
    pagePath: '/pages/...',  // 路由
    text: '首页',
    platform: PlatformEnum.Merchant,
    tabbar: PageTabBarEnum.TabHomePage,
    iconPath: '../../resource/icons/tab-icon-home.png',
    selectedIconPath:  '../../resource/icons/tab-icon-home-check.png'
  }, {
    pagePath: '/pages/...',
    text: '点餐',
    platform: PlatformEnum.Merchant,
    tabbar: PageTabBarEnum.TabOrderDishesPage,
    iconPath: '../../resource/takeout/pdw-tab-order.png',
    selectedIconPath: '../../resource/takeout/pdw-tab-order-check.png'
  }, {
    pagePath: '/pages/...',
    text: '会员',
    platform: PlatformEnum.Merchant,
    tabbar: PageTabBarEnum.TabMemberPage,
    iconPath: '../../resource/icons/tab-icon-member.png',
    selectedIconPath: '../../resource/icons/tab-icon-member-check.png'
  }, {
    pagePath: '/pages/...',
    text: '首页',
    platform: PlatformEnum.Pdw,
    tabbar: PageTabBarEnum.TabPdwHomePage,
    iconPath: '../../resource/takeout/pdw-tab-home.png',
    selectedIconPath: '../../resource/takeout/pdw-tab-home-check.png'
  },  {
    pagePath: '/pages/...',
    text: '订单',
    platform: PlatformEnum.Pdw,
    tabbar: PageTabBarEnum.TabPdwOrderListPage,
    iconPath: '../../resource/takeout/pdw-tab-order.png',
    selectedIconPath: '../../resource/takeout/pdw-tab-order-check.png'
  },  {
    pagePath: '/pages/...',
    text: '我的',
    platform: PlatformEnum.Pdw,
    navigateTo: true,
    tabbar: PageTabBarEnum.TabPdwMemberPage,
    iconPath: '../../resource/takeout/pdw-tab-my.png',
    selectedIconPath: '../../resource/takeout/pdw-tab-my-check.png'
  }
]

class customTabBar extends Component {

  static propTypes = {
    TabBar: PropsType.number,   // 当前tabbar
    onChangeTabbar: PropsType.func,  // 修改tabbar
  }

  state = {
    selected: 0,  // 当前激活的tab下标
    color: '#000',  // 字体颜色
    selectedColor: '#00bab4',  // 激活的字体颜色
    backgroundColor: '#ffffff',  // 背景色
    borderStyle: 'black',   // 边框颜色
  }

  componentDidMount() {
  }

  /*
  * 获取平台类型*/
  IsPdwPlatform = () => {
    return PlatformEnum.Merchant
  }

  /**
   * 获取底部Tab列表
   * */
  getTabList = () => {
    // 这里判断平台来区分需要显示的tabbar  这里IsPdwPlatform 函数需要自己实现
    let platform = this.IsPdwPlatform() ? PlatformEnum.Pdw : PlatformEnum.Merchant;
    let tabs = TabList.filter(item => item.platform === platform);
    let tabList = tabs.map(item => {
      return {...item}
    });
    return tabList;
  }

  render() {
    const {TabBar} = this.props;
    let TabList = this.getTabList();
    return (
      <CoverView className={classnames('custom-tab')}>
        {
          TabList.map((item, index) => {
            return <CoverView className='custom-tab-item' onClick={this.switchTab.bind(this, item)}
                              data-path={item.pagePath} key={index}>
              <CoverImage className='custom-tab-item-img'
                          src={TabBar === item.tabbar ? item.selectedIconPath : item.iconPath}/>
              <CoverView className='custom-tab-item-text'
                         style={{color: TabBar === item.tabbar ? this.state.selectedColor : this.state.color}}>
                {item.text}
              </CoverView>
            </CoverView>
          })
        }
      </CoverView>
    )
  }

  /**
   * 切换Tab
   * */
  switchTab = (tabData) => {
    let that = this;
    const {pagePath, navigateTo, tabbar} = tabData;
    if (navigateTo) {
      Taro.navigateTo({
        url: pagePath
      })
    } else {
      Taro.switchTab({
        url: pagePath,
        success: () => {
          that.props.onChangeTabbar(tabbar);  // 记录当前切换的底部tab
        }
      })
    }
  }
}

const mapStateToProps = (state) => {
  const {TabBar} = state[StoreKeys.GlobalStore];
  return {
    TabBar: TabBar,
  };
};

const mapDispatchToProps = {
  onChangeTabbar    // 记录底部获取焦点的tab
};

export default connect(mapStateToProps, mapDispatchToProps)(customTabBar)
.custom-tab {
  //display: none;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  position: fixed;
  bottom: 0;
  width: 100vw;
  height: 50Px;
  border-radius: 0;
  box-sizing: border-box;
  border-top: 1Px solid #f1f1f1;
  background-color: #FFFFFF;

  &-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;

    &-img {
      margin: 0 auto;
      width: 28PX;
      height: 28Px;
    }

    &-text {
      margin-top: 4Px;
      font-size: 10Px;
    }
  }
}

.show-custom-tab {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}

 

方法二:在页面中使用Taro.hideTabBar() 隐藏底部Tabbar,并创建自定义Tarbar组件,在页面中引入,Tabbar组件就直接使用上面方法一组件CustomTabBar,文件名要大写

同时在app.config.js的tabBar的list配置中,只配置pagePath的路由,否则页面在显示时会先看到配置中的tabbar,然后又显示自定义的,只配置pagePath,底部只会看到一个空白的,

组件的样式要记得添加z-index,否则有可能因为position属性被页面其他组件遮挡

 

 

 

 

方法一与方法二区别不大,方法一好处是不用在页面中引入组件,方法二是组件引入的方式,需要引入

 

标签:Taro,tab,..,pagePath,Tabbar,tabbar,resource,png,自定义
From: https://www.cnblogs.com/wind-wang/p/16798285.html

相关文章

  • 【微信小程序】组件的生命周期及自定义组件
    文章目录​​组件的生命周期​​​​自定义组件的生命周期函数​​​​执行顺序​​​​组件常用的生命周期函数​​​​lifetimes节点​​​​组件所在页面的生命周期函数......
  • CSS自定义字体 竖向偏移怎么办?聊聊字体文件的字体度量、上升、下降
    背景昨天我发布了联机象棋《联机象棋发布!打开URL就能联机对战!观战!单机演练!分享残局!》,有玩家试玩,截图如下:通过截图大小以及里面的emoji我推断出:这是一台WindowsPC。之前我在......
  • 自定义异常和练习自定义异常
    自定义异常java中不同的异常类,分别表示着某一种具体的异常情况那么在开发中总是有些异常情况是SUN没有定义好的此时我们根据自己业务的异常情况来定义异常类例如年龄......
  • Docker | 自定义网络(网关、子网地址)
    了解dockernetwork通过下面的命令来获取帮助dockernetwork--helpCommands:connectConnectacontainertoanetworkcreateCreateanetwork......
  • 四、WinUI3下TitleBar的自定义
    WinUI3下TitleBar的自定义对于Windows软件开发者来说重写标题栏样式是一个很重要的事情,在WPF阶段很多人写出来性能很差的窗口,而且为了适配Win11系统的Snaplayout后性能就......
  • python新类似乎违背了广度优先的执行顺序, 对象自定义计数实例化的多少
    classTSSS():deff1(self):print('fromTSSS')classSSS(TSSS):deff1(self):print('fromSSS')classSS():deff1(self):......
  • Spring Boot:自定义 Whitelabel 错误页面
    一、概述在本文中,我们将研究如何禁用和自定义SpringBoot应用程序的默认错误页面,因为正确的错误处理描述了专业性和质量工作。2.禁用白标错误页面首先,让我们看看如何通......
  • Qt自定义类使用QSS换肤
    文章目录​​一、前言​​​​二、效果展示​​​​三、具体步骤​​​​3.1、自定义继承自QWidget的类​​​​3.2、自定义类添加自定义属性及接口​​​​3.3、完整的时钟......
  • ES 自定义分析器
    分析器的组成一个分析器由三部分组成:CharacterFilter/Tokenizer/TokenFilerCharacterFilters在Tokenizer之前对文本进行处理,可以配置多个CharacterFilter。ES自带......
  • 自定义动画
    defbfi(a):#只能在xxp()中使用li=[]foriina:i.set(sheen_direction=d)......