• 微信小程序 自定义tabBar


    一、官方文档

     自定义 tabBar | 微信开放文档

    根据官方的案例,做了自己的案例测试,效果图如下

    二、在app.json里面添加tabBar配置

    1. "pages":[
    2. "pages/index/index",
    3. "pages/logs/logs",
    4. "pages/goods/index"
    5. ],
    6. "tabBar": {
    7. "custom": true,
    8. "color": "#000000",
    9. "selectedColor": "#000000",
    10. "backgroundColor": "#000000",
    11. "list": [{
    12. "pagePath": "pages/index/index",
    13. "text": "首页"
    14. }, {
    15. "pagePath": "pages/goods/index",
    16. "text": "商品"
    17. }]
    18. }

    配置完成后,不会出现tabBar,需要在custom-tab-bar这个微信小程序已经定义的文件夹,里面添加对应的路径配置

    三、在custom-tab-bar添加配置

    1. 在custom-tab-bar创建如下目录

    1. custom-tab-bar/index.js
    2. custom-tab-bar/index.json
    3. custom-tab-bar/index.wxml
    4. custom-tab-bar/index.wxss

    2.给index.wxml添加tabBar的结构代码  

    1. <cover-view class="tab-bar">
    2. <cover-view class="tab-bar-border">cover-view>
    3. <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    4. <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}">cover-image>
    5. <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}cover-view>
    6. cover-view>
    7. cover-view>

    3. 给index.js 添加数据配置 和 事件方法

    1. Component({
    2. data: {
    3. selected: 0,
    4. color: "#7A7E83",
    5. selectedColor: "#3cc51f",
    6. list: [{
    7. pagePath: "/pages/index/index",
    8. iconPath: "/image/home2_icon.png",
    9. selectedIconPath: "/image/home1_icon.png",
    10. text: "首页"
    11. }, {
    12. pagePath: "/pages/goods/index",
    13. iconPath: "/image/readTrain2_icon.png",
    14. selectedIconPath: "/image/readTrain1_icon.png",
    15. text: "商品"
    16. }]
    17. },
    18. attached() {
    19. },
    20. methods: {
    21. switchTab(e) {
    22. console.log("执行跳转",e);
    23. const data = e.currentTarget.dataset
    24. const url = data.path
    25. wx.switchTab({url})
    26. this.setData({
    27. selected: data.index
    28. })
    29. }
    30. }
    31. })

    4. 给index.wxss 添加样式

    1. /* custom-tab-bar/index.wxss */
    2. .tab-bar {
    3. position: fixed;
    4. bottom: 0;
    5. left: 0;
    6. right: 0;
    7. height: 48px;
    8. background: white;
    9. display: flex;
    10. padding-bottom: env(safe-area-inset-bottom);
    11. }
    12. .tab-bar-border {
    13. background-color: rgba(0, 0, 0, 0.33);
    14. position: absolute;
    15. left: 0;
    16. top: 0;
    17. width: 100%;
    18. height: 1px;
    19. transform: scaleY(0.5);
    20. }
    21. .tab-bar-item {
    22. flex: 1;
    23. text-align: center;
    24. display: flex;
    25. justify-content: center;
    26. align-items: center;
    27. flex-direction: column;
    28. }
    29. .tab-bar-item cover-image {
    30. width: 27px;
    31. height: 27px;
    32. }
    33. .tab-bar-item cover-view {
    34. font-size: 10px;
    35. }

    此时,通过点击切换页面,就可以实现

    =》如果在选择点击切换按钮时,页面没根据tabBar进行切换,问题很可能能在custom-tab-bar目录的index.js 的list数据配置的页面路径,一定要/开头,如/pages/goods/index,这才能找到页面。

    四、tabbar图标切换 要点击两次才能有选中状态

    在`/pages/index目录的index.js文件,的onShow生命周期方法,添加如下代码

    1. onShow() {
    2. if (typeof this.getTabBar === 'function' &&
    3. this.getTabBar()) {
    4. this.getTabBar().setData({
    5. //唯一标识(其它设置不同的整数)
    6. selected: 0
    7. })
    8. }
    9. },

    pages/goods/目录的index.js文件,的onShow生命周期方法,添加如下代码,

    1. onShow() {
    2. if (typeof this.getTabBar === 'function' &&
    3. this.getTabBar()) {
    4. this.getTabBar().setData({
    5. //唯一标识(其它设置不同的整数)
    6. selected: 1
    7. })
    8. }
    9. },

    注意:预览时,提示登陆的用户不是小程序的开发者,可添加appid

  • 相关阅读:
    Flask的 preprocess_request
    android studio启动Task配置
    第四届辽宁省大学生程序设计竞赛(正式赛)A B H F M C
    全球首个“AI程序员”Deven诞生,真的能替代人类程序员吗?
    向已有项目添加LICENSE
    Linux下怎样使用core文件查看异常崩溃的程序问题
    Vuex有几种属性以及它们的意义
    MongoDB数据迁移之迁移工具Kettle
    当电脑接双屏扩展显示器时,系统崩溃,QQ浏览器出现显示异常,通过如下方法使之正常
    [附源码]Python计算机毕业设计Django的花店售卖系统的设计与实现
  • 原文地址:https://blog.csdn.net/tengyuxin/article/details/126105067