当前位置:   article > 正文

微信小程序——自定义底部tabBar(以及解决图标点击两次才能选中状态)_uniapp使用自定义custom-tab-bar点击两次才选中

uniapp使用自定义custom-tab-bar点击两次才选中

我觉得还有有必要写一下这篇文章,因为this.getTabBar().setData({})我找了好多解决方案他们都是采用这同一个解决方式,可这个方法并没有帮我解决我也很无语。

不过好在终于解决了,写出来帮助那些跟我当时一样等待解决的人,下面直接上代码

与(原):微信小程序——自定义底部tabBar_custom-tab-bar-CSDN博客    略有不同

具体视频如下:

解决微信小程序自定义tarbar - 点击两次图标才能选中

一:自定义tarbar 

1.在根目录下创建custom-tab-bar [ 名字不要变,必须是这个 ]

2.在全局 app.js 中开启"custom": true,并运用组件

  1. "tabBar":{
  2. "custom": true,
  3. "list": [{
  4. "pagePath": "pages/index/index",
  5. "iconPath": "/images/ico_home_tab_juchang_nor 2@2x.png",
  6. "selectedIconPath": "/images/ico_home_tab_juchang_pre 2@2x.png",
  7. "text": "首页"
  8. }, {
  9. "pagePath": "pages/commdt/index",
  10. "iconPath": "/images/ico_home_tab_juchang_nor 3@2x.png",
  11. "selectedIconPath": "/images/ico_home_tab_juchang_pre 3@2x.png",
  12. "text": "视频"
  13. },
  14. {
  15. "pagePath": "pages/three/three",
  16. "iconPath": "/images/ico_home_tab_juchang_nor@2x.png",
  17. "selectedIconPath": "/images/ico_home_tab_juchang_pre@2x.png",
  18. "text": "充值"
  19. }]
  20. },
  21. "usingComponents": {
  22. "custom-tab-bar":"/custom-tab-bar/custom-tab-bar"
  23. },

3. custom-tab-bar.js [ 不同的部分已注释标出 ]

  1. // custom-tab-bar/custom-tab-bar.js
  2. Component({
  3. // 1.不同部分定义 properties
  4. properties:{
  5. selected:{
  6. type:Number,
  7. value:null
  8. }
  9. },
  10. data: {
  11. color: "#7A7E83",
  12. selectedColor: "#3cc51f",
  13. list: [{
  14. pagePath: "/pages/index/index",
  15. iconPath: "/images/ico_home_tab_juchang_nor 2@2x.png",
  16. selectedIconPath: "/images/ico_home_tab_juchang_pre 2@2x.png",
  17. text: "首页"
  18. }, {
  19. pagePath: "/pages/commdt/index",
  20. iconPath: "/images/ico_home_tab_juchang_nor 3@2x.png",
  21. selectedIconPath: "/images/ico_home_tab_juchang_pre 3@2x.png",
  22. text: "视频"
  23. },
  24. {
  25. pagePath: "/pages/three/three",
  26. iconPath: "/images/ico_home_tab_juchang_nor@2x.png",
  27. selectedIconPath: "/images/ico_home_tab_juchang_pre@2x.png",
  28. text: "充值"
  29. }]
  30. },
  31. attached() {
  32. },
  33. methods: {
  34. switchTab(e) {
  35. console.log("执行跳转",e);
  36. const data = e.currentTarget.dataset
  37. const url = data.path
  38. wx.switchTab({url})
  39. this.setData({
  40. // 2.不同部分使用 properties 中定义数据
  41. selected: this.properties.selected
  42. })
  43. }
  44. }
  45. })

4.custom-tab-bar.wxml

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

5.custom-tab-bar.wxss

  1. /* custom-tab-bar/custom-tab-bar.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. }

二:其他页面引入使用 - 【这里以index页为例

对的,我们这里解决方法就是用组件方式,而非this.getTabBar().setData({})这种获取然后再塞值,我是没有成功解决点击两次才能图片选中,不然也不会有此片文章
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/869051

推荐阅读
相关标签