收藏
回答

如何解决自定义tabbar下拉到页面最底部出现两个以及切换界面时闪烁问题?

自定义tabbar拉到页面最底部往上就会出现两个tabbar,且在切换界面时tabbar图标会闪烁,用iPhone6/7/8机型,请各位大神们帮忙看看谢谢!

app.json代码

........
"tabBar": {
    "custom": true,
    "selectedColor": "#7cb342",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "器材",
        "iconPath": "/static/tabbar/icon_index1.png",
        "selectedIconPath": "/static/tabbar/icon_index2.png"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "我的",
        "iconPath": "/static/tabbar/icon_logs1.png",
        "selectedIconPath": "/static/tabbar/icon_logs2.png"
      },
      {
        "pagePath": "pages/admin/aindex",
        "text": "管理",
        "iconPath": "/static/tabbar/aindex1.png",
        "selectedIconPath": "/static/tabbar/aindex2.png"
      },
      {
        "pagePath": "pages/admin/apply",
        "text": "申请单",
        "iconPath": "/static/tabbar/icon_index1.png",
        "selectedIconPath": "/static/tabbar/icon_index2.png"
      },
      {
        "pagePath": "pages/admin/my",
        "text": "我的",
        "iconPath": "/static/tabbar/icon_logs1.png",
        "selectedIconPath": "/static/tabbar/icon_logs2.png"
      }
    ]
  },
  "sitemapLocation": "sitemap.json"


custom-tab-bar中index.js代码

Component({
  data: {
    selected: 0,
    color: "#000000",
    role: '',
    selectedColor: "#1396DB",
    allList: [{
      list1: [{
        "pagePath": "/pages/index/index",
        "text": "器材",
        "iconPath": "/static/tabbar/icon_index1.png",
        "selectedIconPath": "/static/tabbar/icon_index2.png"
      },
      {
        "pagePath": "/pages/logs/logs",
        "text": "我的",
        "iconPath": "/static/tabbar/icon_logs1.png",
        "selectedIconPath": "/static/tabbar/icon_logs2.png"
      }],

      list2: [{
        "pagePath": "/pages/admin/aindex",
        "text": "管理",
        "iconPath": "/static/tabbar/aindex1.png",
        "selectedIconPath": "/static/tabbar/aindex2.png"
      },
      {
        "pagePath": "/pages/admin/apply",
        "text": "申请单",
        "iconPath": "/static/tabbar/icon_index1.png",
        "selectedIconPath": "/static/tabbar/icon_index2.png"
      },
      {
        "pagePath": "/pages/admin/my",
        "text": "我的",
        "iconPath": "/static/tabbar/icon_logs1.png",
        "selectedIconPath": "/static/tabbar/icon_logs2.png"
      }],
    }],
    list: []
  },
  attached() {
    const role = wx.getStorageSync("userInfo").role
    console.log(role);
    if (role == 'user') {
      this.setData({
        list: this.data.allList[0].list1
      })
    }else if(role == 'admin'){
      this.setData({
        list: this.data.allList[0].list2
      })
    }
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      console.log(url);
      wx.switchTab({ url })
      this.setData({
        selected: data.index
      })
    }
  },
})

custom-tab-bar/index.wxml代码

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

custom-tab-bar/index.wxss代码

/* custom-tab-bar/index.wxss */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item .cover-image {
  width: 44rpx;
  height: 44rpx;
}

.tab-bar-item .cover-view {
  margin-top: 8rpx;
  font-size: 24rpx;
}
回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容