uniapp 实现tabBar图标凸起的两种方式(App端)

在这里插入图片描述

开发中客户可能会需求App端的tabBar中间的图标凸起,以实现更好的审美观。但如何实现呢?我这里推荐的两种方式,第一种是查阅相关例子按需配置隐藏于显示实现的,第二种是自己按官方文档配置midButton 自定义实现的。
uni-app版本更新到2.3.5后App平台 重构 tabBar,原生支持 midButton(中间凸起),支持高度调节(App、H5默认高度统一为50px),降低内存占用,避免iOS白屏。

github: https://github.com/Front97721/uniapp-tabBarRaised.git

先看效果吧!!嘿嘿!!
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
按需配置隐藏于显示

按平常一样配置pages.json

"tabBar": {
		"color": "#999999",
		"selectedColor": "#FA4740",
		"borderStyle": "black",
		"backgroundColor": "#ffffff",
		"list":[{
				"fontSize": "11px",
				"pagePath": "pages/index/index",
				"iconPath": "static/logo.png",
				"selectedIconPath": "static/logo.png",
				"text": "首页"
			},
			{
				"pagePath": "pages/classification/classification",
				"iconPath": "static/logo.png",
				"selectedIconPath": "static/logo.png",
				"text": "分类"
			},
			{
				"pagePath": "pages/circle/circle",
				"iconPath": "static/circle.png",
				"selectedIconPath": "static/circle.png",
				"text": "圈子"
			},
			{
				"pagePath": "pages/order/order",
				"iconPath": "static/logo.png",
				"selectedIconPath": "static/logo.png",
				"text": "购物车"
			},
			{
				"pagePath": "pages/my/my",
				"iconPath": "static/logo.png",
				"selectedIconPath": "static/logo.png",
				"text": "我的"
			}]
	}

加载图片(index.vue)

onLoad() {
    bitmap = new plus.nativeObj.Bitmap('bmp1');  //新增原生图片
    //bitmap.load('static/circle.png', function() {}, function(e) {});  //普通写法
    //兼容ios端
    bitmap.loadBase64Data('base64图片地址', function() {}, function(e) {});
    this.createtab();
}

这为了兼容IOS端,需要用base64图片地址(转换工具),我尝试了一下用GIF动态图也OK的,就是转base64时编码好多,卡卡的,不过可以用普通写法(非IOS端)。

设置凸起按钮样式位置(index.vue)

methods: {
	createtab: function(){
		// 设置水平居中位置
		var leftPos = Math.ceil((plus.screen.resolutionWidth - 50) / 2);
		var view = new plus.nativeObj.View('icon', {
			bottom: '19px',
			left: leftPos + 'px',
			width: '50px',
			height: '50px'
		});
		//使用canvas画布把图标画到tabbar对应位置上
		view.drawBitmap(bitmap, {
			tag: 'font',
			id: 'icon',
			src: '/static/app-tabber/app-tabber.png',
			position: {
				top: '0px',
				left: '5px',
				width: '50px',
				height: '100%'
			}
		});
		//监听界面跳转
		view.addEventListener("click", function(e) {
			uni.switchTab({
				url: '/pages/circle/circle'
			})
		}, false);
		view.show();
	}
}

显示凸起图标(index.vue)

onShow() { //显示突起图标
	var icon = plus.nativeObj.View.getViewById("icon");  //根据图标id取到图片
	if (icon) {
		setTimeout(function(){
			icon.show();
		},50)
	}
},

二级页面隐藏tabBar
方式一:

() {
    /* 隐藏tabBar图标 */
    var icon = plus.nativeObj.View.getViewById("icon");
    setTimeout(function() {
        icon.hide();
    }, 100);
}

方式二:
以前遇到,好像page.json中配置,忘了!持续更新------

配置midButton

参照 官方文档

midButton就是中间按钮,有的文档说仅在 list 项为偶数时有效,其实不然。

配置midButton及路由(page.json)

//tabBar下配置midButton,设置图标的大小并编译到App端
// #ifdef APP-PLUS
	"midButton": {
		  "pagePath": "pages/circle/circle",
		  "iconPath": "static/circle.png",
		  "width": "60px",
		  "height": "65px",
		  "iconWidth": "40px",
		  "text": "圈子"
},
// #endif
//配置凸起图标路由及编译到App端
// #ifndef APP-PLUS
	{
		"pagePath": "pages/circle/circle",
		"fontSize":"11px",
		"iconPath": "static/circle.png",
		"selectedIconPath": "static/circle.png",
		"text": "圈子"
	},
// #endif

配置二级界面的跳转及隐藏tabBar(App.vue)

uni.onTabBarMidButtonTap(function() {
					 	uni.navigateTo({
					 		url: '/pages/circle/circle'
					 	})
					 })

总结:

  • 按需配置隐藏于显示,主要是在首页配置对应的图标加载、tabbar图标凸起及显示,子页面配置对应的tabbar隐藏。不好的地方就是每个子页面都要配置对应的tabbar隐藏。
  • 配置midButton,主要是page.json配置midButton中间件、相关路由和图标凸起及相关编译平台。App.vue配置凸起图标的二级跳转。
  • 都说uniapp是万能的—所有的东西都不是万能,所以相信以后会慢慢实现更多的。
    在这里插入图片描述
  • 0
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
uniapp是一种跨平台的开发框架,它结合了vue.js和微信小程序的优点,并可在多个平台上运行。实现tabbar凸起的方法有很多种,下面是一种常用的做法: 1. 在`uni-tabbar`组件上添加一个`custom`属性,并将其设为`true` 。 ``` <uni-tabbar custom="true"> ``` 2. 在`uni-tabbar`组件内部添加一个`uni-icons`图标组件,并设置其属性使它放在tabbar中间。 ``` <uni-tabbar custom="true"> <uni-tabbar-item iconPath="static/tabbar/icon1.png" selectedIconPath="static/tabbar/icon1-active.png" text="Tab 1"></uni-tabbar-item> <uni-tabbar-item iconPath="static/tabbar/icon2.png" selectedIconPath="static/tabbar/icon2-active.png" text="Tab 2"></uni-tabbar-item> <uni-icons type="add" size="40" color="#ffffff" style="position: absolute; top: -30px; left: calc(50% - 20px);"></uni-icons> <uni-tabbar-item iconPath="static/tabbar/icon3.png" selectedIconPath="static/tabbar/icon3-active.png" text="Tab 3"></uni-tabbar-item> <uni-tabbar-item iconPath="static/tabbar/icon4.png" selectedIconPath="static/tabbar/icon4-active.png" text="Tab 4"></uni-tabbar-item> </uni-tabbar> ``` 3. 通过CSS样式将`uni-icons`组件移到tabbar顶部,并使其大小适合tabbar的大小。 ``` .uni-tabbar.uni-tabbar-custom { padding-bottom: 120rpx; position: relative; } .uni-icons { width: 80rpx; height: 80rpx; line-height: 80rpx; text-align: center; background-color: #007aff; z-index: 999; border-radius: 40rpx; box-shadow: 0 1rpx 1rpx rgba(0, 0, 0, 0.1); } ``` 这是一种简单的实现方法,可以适用于大多数情况。如果需要进一步优化,可以使用更高级的技术,如自定义组件、自定义样式等等来实现。无论哪种方式,都可以根据项目的需求自由选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值