【uni-app】app界面TabBar中间大图标设置的两种方法:midButton的使用;iconfont的使用

前言

最近在写app的时候,需要将tabbar导航栏中间的发布按钮设置为大图标,于是就和我另一个伙伴一起研究这个功能。有两种实现方式:①通过设置midButton属性放大图标,通过调用uni.onTabBarMidButtonTap实现页面跳转;②通过设置tabbar-list对应的iconfont属性,将图标转换为字体图标。具体实现如下。

方法一:midButton的使用

官方文档:pages.json页面路由–tabbar

官方描述:

在这里插入图片描述
在这里插入图片描述

官方文档:uni.onTabBarMidButtonTap(CALLBACK)

在这里插入图片描述

使用说明:

uniapp官方推出midButton属性,可以用来实现中间按钮样式的自定义,使用时需要注意list必须是偶数,也就是在tabbar-list数组中定义的导航数为偶数。且midButton中没有pagePath的属性,所以我们需要调用uni.onTabBarMidButtonTap()来实现页面跳转(uni.onTabBarMidButtonTap只支持app,所以使用midButton需要跳转的话,记得加上条件判断)。

代码展示:

pages.json

{
...
"tabBar": {
    "custom": true,
    "color": "#000000",
    "backgroundColor": "#f0f0f0",
    "selectedColor": "#a9f3f8",
    "borderStyle": "#f0f0f0",
    "list": [{
        "pagePath": "pages/examdata/examdata",
        "text": "资料",
        "iconPath": "/static/tabBar/examdata.png",
        "selectedIconPath": "/static/tabBar/examdataActive.png"
      },
      {
        "pagePath": "pages/treehole/treehole",
        "text": "树洞",
        "iconPath": "/static/tabBar/treehole.png",
        "selectedIconPath": "/static/tabBar/treeholeActive.png"
      },
      {
        "pagePath": "pages/tutorship/tutorship",
        "text": "辅导",
        "iconPath": "/static/tabBar/tutorship.png",
        "selectedIconPath": "/static/tabBar/tutorshipActive.png"
      },
      {
        "pagePath": "pages/mypage/mypage",
        "text": "我的",
        "iconfont": {
          
        },
        "iconPath": "/static/tabBar/mypage.png",
        "selectedIconPath": "/static/tabBar/mypageActive.png"
      }
    ],
    "midButton": {
      "width": "60px",
      "height": "60px",
      "iconWidth": "50px",
      "iconPath": "/static/tabBar/release.png"
    }
  },
  ...
}

app.vue

<script>
  export default {
    onLaunch: function() {
      console.log('App Launch')
      //在
      uni.onTabBarMidButtonTap(() => {
        uni.navigateTo({
          url: '/pages/release/release',
        });
      })
    },
    onShow: function() {
      console.log('App Show')
    },
    onHide: function() {
      console.log('App Hide')
    }
  }
</script>

效果展示:

在这里插入图片描述

方法二:iconfont的使用

官方文档: css语法-字体图标

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

使用步骤:

iconfont–阿里巴巴图标库

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

此处需要注意,从阿里巴巴图标库中复制下来的图标代码是&#xe66f;,需要将&#xe转换为\ue

在这里插入图片描述

使用说明:

以上我们会发现用iconfont可以成功在内置浏览器显示,但是在手机调试或者打包成app的时候这种样式会出现一些问题。因为app版本适配问题,iconfont对app版本要求比较高,适用于3.4.4以上版本。

在这里插入图片描述

在这里插入图片描述

以上就是我们在实现功能过程中,总结的两种方法。

如有误,请指正!

在这里插入图片描述

  • 16
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
在 `uni-app` 中,可以通过以下步骤单独设置 `tabbar` 中某个图标的大小: 1. 在 `tabbar` 组件中,使用 `iconPath` 和 `selectedIconPath` 属性分别设置图标的路径和选中时的图标路径。 2. 在 `tabbar` 组件外部,使用 `wxss` 文件来设置图标的大小。 3. 针对需要单独设置大小的图标,可以在 `wxss` 文件中对其进行单独设置。 举个例子,假设我们有一个 `tabbar` 组件,其中有三个图标需要设置大小为 30px,而另一个图标需要设置大小为 40px。那么我们可以按照以下方式进行设置: 1. 在 `tabbar` 组件中,设置各个图标的路径: ```html <tabbar> <tabbar-item iconPath="/static/tabbar/home.png" selectedIconPath="/static/tabbar/home-active.png"></tabbar-item> <tabbar-item iconPath="/static/tabbar/category.png" selectedIconPath="/static/tabbar/category-active.png"></tabbar-item> <tabbar-item iconPath="/static/tabbar/cart.png" selectedIconPath="/static/tabbar/cart-active.png"></tabbar-item> <tabbar-item iconPath="/static/tabbar/mine.png" selectedIconPath="/static/tabbar/mine-active.png"></tabbar-item> </tabbar> ``` 2. 在 `wxss` 文件中设置图标的大小: ```css /* 设置所有图标的大小为 30px */ .tabbar-item__icon { width: 30px; height: 30px; } /* 单独设置某个图标的大小为 40px */ .tabbar-item__icon--mine { width: 40px; height: 40px; } ``` 3. 在 `tabbar` 组件中,针对需要单独设置大小的图标,加上对应的 `class`: ```html <tabbar> <tabbar-item iconPath="/static/tabbar/home.png" selectedIconPath="/static/tabbar/home-active.png"></tabbar-item> <tabbar-item iconPath="/static/tabbar/category.png" selectedIconPath="/static/tabbar/category-active.png"></tabbar-item> <tabbar-item iconPath="/static/tabbar/cart.png" selectedIconPath="/static/tabbar/cart-active.png"></tabbar-item> <tabbar-item iconPath="/static/tabbar/mine.png" selectedIconPath="/static/tabbar/mine-active.png" class="tabbar-item__icon--mine"></tabbar-item> </tabbar> ``` 这样,我们就成功地单独设置了 `tabbar` 中某个图标的大小。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值