当前位置:   article > 正文

uniapp 自定义底部tabbar,支持底部安全区和占位符_uniapp tabbar 底部安全区域

uniapp tabbar 底部安全区域

 tabbar组件:

  1. <template>
  2. <view :style="'height: '+placeholder+'rpx'">
  3. <view class="tabbar-box">
  4. <view class="tabbar">
  5. <view class="item" v-for="(item,i) in tabbar" @click="toPage(item.pagePath)">
  6. <text class="t-icon" :class="i==index?item.seletedIcon:item.icon"></text>
  7. <view class="title">{{item.text}}</view>
  8. </view>
  9. </view>
  10. <view class="placeholder" :style="'height:'+safeAreaInsetBottom+'rpx;'"></view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import util from '../../utils/util';
  16. export default {
  17. data() {
  18. return {
  19. tabbar: [{
  20. "pagePath": "/pages/index/index",
  21. "icon": "t-icon-shouye-weixuanzhongtai",
  22. "seletedIcon": 't-icon-shouye-xuanzhongtai',
  23. "text": "首页"
  24. },
  25. {
  26. "pagePath": "/pages/my/my",
  27. "icon": "t-icon-wode-weixuanzhongtai",
  28. "seletedIcon": 't-icon-wode-xuanzhongtai',
  29. "text": "我的"
  30. }
  31. ],
  32. //底部安全区域高度
  33. safeAreaInsetBottom: 0,
  34. //底部占位符高度
  35. placeholder: 0
  36. }
  37. },
  38. props: {
  39. //底部选项卡索引,用于icon高亮
  40. index: {
  41. type: String || Number,
  42. default: 0
  43. },
  44. },
  45. mounted() {
  46. this.safeAreaInsetBottom = uni.getSystemInfoSync().safeAreaInsets.bottom * 2
  47. this.$nextTick(() => {
  48. util.computedElementHeight('.tabbar-box').then(res => {
  49. this.placeholder = this.safeAreaInsetBottom + res
  50. })
  51. })
  52. },
  53. methods: {
  54. toPage(url) {
  55. uni.switchTab({
  56. url: url
  57. })
  58. }
  59. }
  60. }
  61. </script>
  62. <style scoped lang="scss">
  63. .tabbar-box {
  64. position: fixed;
  65. width: 100%;
  66. background: #FFF;
  67. z-index: 999;
  68. bottom: 0;
  69. .tabbar {
  70. height: 100rpx;
  71. display: flex;
  72. justify-content: space-around;
  73. align-content: center;
  74. .item {
  75. display: flex;
  76. flex-direction: column;
  77. align-items: center;
  78. justify-content: center;
  79. .t-icon {
  80. width: 50rpx;
  81. height: 50rpx;
  82. }
  83. .title {
  84. color: #8C8C8C;
  85. font-size: 20rpx;
  86. }
  87. }
  88. }
  89. }
  90. .placeholder {
  91. background-color: #FFF;
  92. }
  93. </style>

utils工具类

  1. //动态计算元素高度,返回值单位rpx,调用前一定要加nextTick
  2. const computedElementHeight = async (element) => {
  3. return new Promise((resolve, reject) => {
  4. let query = uni.createSelectorQuery();
  5. query.select(element).boundingClientRect(rect => {
  6. let clientHeight = rect.height;
  7. let clientWidth = rect.width;
  8. let ratio = 750 / clientWidth;
  9. let height = clientHeight * ratio;
  10. resolve(height);
  11. }).exec();
  12. })
  13. }
  14. export default {
  15. computedElementHeight
  16. }

 配置:

  1. //pages.json
  2. "tabBar": {
  3. "custom": true,
  4. "list": [{
  5. "pagePath": "pages/index/index"
  6. },
  7. {
  8. "pagePath": "pages/my/my"
  9. }
  10. ]
  11. },
  1. //App.vue
  2. onLaunch: function() {
  3. uni.hideTabBar()
  4. }

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