小程序(三)配置tabbar及自定义tabbar样式

关于tabbar部分,官方文档是有明确的说明的,当然,我这里是不存在把官方文档给你复制一遍的情况。我大概把我再看官方文档过程中遇到的坑,大概复述一下。

一:配置tabbar

这个主要是使用小程序自带的tabbar,在项目根目录下的app.json中配置,这个简单配置一下就可以了。

这里放一下我的app.json配置:

{
  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/article/article",
    "pages/board/board",
    "pages/remark/remark"
  ],
  "tabBar": {
    "selectedColor": "#31869B",
    "list": [
      {
        "current": 0,
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/images/index-select.png",
        "selectedIconPath": "/images/index-selected.png"
      },
      {
        "current": 1,
        "pagePath": "pages/remark/remark",
        "text": "随言碎语",
        "iconPath": "/images/sui-select.png",
        "selectedIconPath": "/images/sui-selected.png"
      },
      {
        "current": 2,
        "pagePath": "pages/board/board",
        "text": "留言板",
        "iconPath": "/images/board-select.png",
        "selectedIconPath": "/images/board-selected.png"
      }
    ]
  },
  "usingComponents": {
    "weuitabbar": "/miniprogram_npm/weui-miniprogram/tabbar/tabbar"
  },
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}

更详细的tabbar配置属性说明,请移步官方文档《tabbar

最后效果如下图所示:

在这里插入图片描述

二:自定义tabbar

有些时候,官方的样式并不能满足我们的要求,这个时候,我们就需要自定义tabbar的样式,当然,这个官方也是有说明的,只是,说明比较简单,官方给了一段示例代码。具体请移步官方文档《自定义tabbar

然后,我这里大概分享下我是怎么写的:

App.json中,tabbar有一个custom属性,这个很重要,如果将这个属性设置为true,那么程序会默认使用自定义的tabbar。

那么我的app.json中的tabbar部分代码,就变成了下面的这个样子:

"tabBar": {
    "custom": true,    
  },
 

在小程序根目录下创建目录custom-tab-bar,名字一定要是这个,别瞎起名。

在目录中创建组件index,注意,这里一定要注意,这里创建的是组件,而不是页面,虽然他们都是四个文件,但是他们是完全不一样的。

创建成功之后,文件目录如下图所示:

在这里插入图片描述

然后,我们开始编写四个文件中的代码:

(1):index.js

Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#31869B",
    fontWeight:'bold',
    "list": [
      {
        "current": 0,
        "pagePath": "/pages/index/index",
        "text": "首页",
        "iconPath": "/images/index-select.png",
        "selectedIconPath": "/images/index-selected.png"
      },
      {
        "current": 1,
        "pagePath": "/pages/remark/remark",
        "text": "随言碎语",
        "iconPath": "/images/sui-select.png",
        "selectedIconPath": "/images/sui-selected.png"
      },
      {
        "current": 2,
        "pagePath": "/pages/board/board",
        "text": "留言板",
        "iconPath": "/images/board-select.png",
        "selectedIconPath": "/images/board-selected.png"
      }
    ]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      console.log(e);
      const idx = e.currentTarget.dataset.index
      const path = e.currentTarget.dataset.path
      // this.setData({
      //   selected: idx
      // })
      wx.switchTab({
        url: path,
      });
    }
  }
})

(2):index.json

这里边本来我打算用weui的tabbar组件来着,但是目前还没玩明白,这个回头再说。

{
  "component": true,
  "usingComponents": {
    "weuitabbar": "/miniprogram_npm/weui-miniprogram/tabbar/tabbar"
  }
}

(3):index.wxml

<!-- 自定义tab -->
<view class="tab-bar">
  <view wx:for="{{list}}" wx:key="index" class="top_view" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab" >
    <view>
      <image class="img_" src="{{selected === index ?item.selectedIconPath : item.iconPath}}"></image>
    </view>
    <view style="color: {{selected === index ?selectedColor : color}}">
        {{item.text}}
    </view>
  </view>
</view>

(4):index.wxss

.top_view{
  float:left;width:33%;text-align:center; font-size:12px;
}
.img_{
  width:28px;height:28px;
}
.tab-bar{
  width:100%;
  position: fixed;
  bottom:0;
  padding:10rpx;
  margin-left:-4rpx;
  background:#FFFFFF;
  font-size:20rpx;
  color:#fcf7f7;
  box-shadow: 6rpx 6rpx 6rpx 6rpx rgb(248, 245, 245);
}

(5):这个不单独指哪一个页面:

在tabBar跳转的页面,也就是tab 选中状态下,要在当前页面下,通过 getTabBar 接口获取组件实例,并调用 setData 更新选中态。

举个简单的例子,比如现在我从其他页跳回首页,那么我需要在首页的index.js中的代码应该是如下的样子:

const app = getApp()
Page({
  data: {
    
  },
  onShow(){
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2    // 根据tab的索引值设置
      }) 
    }
  }
})

这样操作完成之后,效果如下图所示:
在这里插入图片描述

但是吧,还有一个问题,来回切换的时候,图标会闪烁,这个使用官方给的这个方法,好像是没有什么太好的解决办法。

如果你有好的解决方式,请在下方评论。

文末有我开发时写的源码。

有好的建议,请在下方输入你的评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问我的小程序:
在这里插入图片描述

  • 14
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
小程序分包是指将一个大型小程序分成多个子包,以便更快地加载应用程序。主包是小程序的初始包,而子包是通过动态加载实现的。常见的分包场景包括将不经常使用的功能或页面放在子包中,以减少主包的大小和加载时间。 自定义tabbar是指开发者可以自定义小程序底部的导航栏。开发者可以按照自己的需求定制导航栏的样式和功能,例如更改图标、更改文字、添加新的tab等。 在小程序中,开发者可以使用wx.loadSubPackage()方法来动态加载子包。在app.json文件中,可以使用"subpackages"字段声明子包的路径和名称。例如: ``` { "subpackages": [ { "root": "subpackage1", "name": "subpackage1" }, { "root": "subpackage2", "name": "subpackage2" } ] } ``` 在自定义tabbar时,开发者需要在app.json文件中的"tabBar"字段中定义tabBar样式和功能。例如: ``` { "tabBar": { "custom": true, "color": "#999", "selectedColor": "#333", "backgroundColor": "#fff", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/home.png", "selectedIconPath": "images/home-active.png" }, { "pagePath": "pages/my/my", "text": "我的", "iconPath": "images/my.png", "selectedIconPath": "images/my-active.png" } ] } } ``` 其中,"custom"字段需要设置为true,表示使用自定义tabBar。"color"字段表示未选中tab的颜色,"selectedColor"字段表示选中tab的颜色,"backgroundColor"字段表示tabBar的背景颜色。"list"字段中每个对象表示一个tab,包括"pagePath"字段表示对应的页面路径,"text"字段表示tab的文字,"iconPath"字段表示tab的图标路径,"selectedIconPath"字段表示选中tab的图标路径。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值