当前位置:   article > 正文

uniapp 如何自定义底部TabBar 且还能使用 uni.switchTab

uni.switchtab
1.隐藏tabBar

        在pages.json文件下 tabBar选项下有个 height配置项设置为0,这个时候uniapp自带的tabBar已经不会在显示了,当是你还是得需要吧你自定义tabBar所需的页面放进来

  

2.创建tabBar文件

        建议在你的项目下面创建一个components文件夹专门放你的自定义组件,然后在新建一个TabBar文件,这里的样式文件可以按照自己的喜好使用scss、less、css文件

直接废话不多说上代码

这个是index.vue文件下的代码 

  1. <template>
  2. <view style="height: 180rpx;">
  3. <view class="content" :style="{zIndex}">
  4. <view class="tabber_box">
  5. <view v-for="(item,index) in tabBars" :key="index"
  6. :style="{width:`${ratio}%`,display:'flex',justifyContent:'space-around',margin: '24rpx 0'}">
  7. <view class="tabber_item" @click="onNav(item.pagePath)">
  8. <image v-if="currentPath === item.pagePath" :src="item.iconPath"></image>
  9. <image v-else :src="item.selectedIconPath"></image>
  10. <text
  11. :style="{color:currentPath === item.pagePath ? item.selectedColor : item.color}">{{item.name}}</text>
  12. </view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. props: {
  21. currentPath: { // 当前页面路径
  22. type: String,
  23. default: '/pages/index/index',
  24. },
  25. zIndex: { // 界面层级
  26. type: Number,
  27. default: 10
  28. }
  29. },
  30. data() {
  31. const color = '#FFFFFF66';
  32. const selectedColor = '#FFF';
  33. return {
  34. tabBars: [{
  35. name: '首页',
  36. iconPath: '../../static/tabbar/index.png',
  37. selectedIconPath: '../../static/tabbar/selectd_index.png',
  38. pagePath: '/pages/index/index',
  39. color,
  40. selectedColor,
  41. },
  42. {
  43. name: '邀请',
  44. iconPath: '../../static/tabbar/lnvite.png',
  45. selectedIconPath: '../../static/tabbar/selectd_lnvite.png',
  46. pagePath: '/pages/lnvite/index',
  47. color,
  48. selectedColor,
  49. },
  50. {
  51. name: 'VIP',
  52. iconPath: '../../static/tabbar/vip.png',
  53. selectedIconPath: '../../static/tabbar/selectd_vip.png',
  54. pagePath: '/pages/vip/index',
  55. color,
  56. selectedColor,
  57. },
  58. {
  59. name: '我的',
  60. iconPath: '../../static/tabbar/my.png',
  61. selectedIconPath: '../../static/tabbar/selectd_my.png',
  62. pagePath: '/pages/my/index',
  63. color,
  64. selectedColor,
  65. },
  66. ],
  67. ratio: 0,
  68. isLogin: false,
  69. }
  70. },
  71. mounted() {
  72. this.ratio = 100 / this.tabBars.length;
  73. },
  74. methods: {
  75. onNav(url) {
  76. if (this.currentPath !== url) uni.switchTab({
  77. url
  78. });
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. @import './index.scss';
  85. </style>

这个是index.scss文件下的代码

  1. .content {
  2. position: fixed;
  3. bottom: 0;
  4. width: 100%;
  5. .tabber_box {
  6. display: flex;
  7. flex-direction: row;
  8. align-items: center;
  9. background: #151d33;
  10. padding-bottom: calc(env(safe-area-inset-bottom) - 48rpx) ; // 适配iphoneX的底部
  11. padding-bottom: calc(env(safe-area-inset-bottom) - 48rpx); /*兼容 IOS>11.2*/
  12. image {
  13. width: 56rpx;
  14. height: 56rpx;
  15. // margin-bottom: 16rpx;
  16. }
  17. .tabber_item {
  18. display: flex;
  19. flex-direction: column;
  20. align-items: center;
  21. font-size: 28rpx;
  22. }
  23. }
  24. }
3.引入到全局

在main.js / main.ts文件下 把写好的TabBar组件注册到全局中

4.如何使用

如果你的页面路径当前在 /pages/index/index 就引入在映入,同理你需要在那个页面显示切选择状态是这个页面 就吧currentPath 这个参数给当前页面路径

最终效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/770069
推荐阅读
相关标签
  

闽ICP备14008679号