plus.nativeObj.View封装自动消失提示框

背景:uniapp项目中使用plus.nativeUI.toast提示框,小米10出现了样式异常,消息框宽度和高度都很大,且无圆角很丑,如下:
在这里插入图片描述
查询相关BUG仅2020年有一个帖子反馈,但显示已解决无法回复,也无其他最新反馈,考虑到官方处理问题周期较长,先用plus.nativeObj.View代替实现提示框。

封装了一个Toast.js:

import message from "../store/modules/message";

const Toast = ({
	message,
	type = 'success'
}) => {
	if (!message) {
		return
	}
	// primary  success  danger  warning
	let types = {
		success: {
			icon: '/static/images/content/success.png',
			background: '#292D32',
			color: '#FFFFFF;'
		}
	}
	const styles = types[type];
	const {
		statusBarHeight
	} = uni.getSystemInfoSync();

	const x = 32; // 计算宽度 32字体加字间距大小 单位rpx
	let w = 128 + message.length * x + 50;
	const view = new plus.nativeObj.View('toast', {
		top: `${uni.upx2px(statusBarHeight + 80)}px`,
		left: `${uni.upx2px((750 - w) / 2)}px`,
		height: `${uni.upx2px(108)}px`,
		width: `${uni.upx2px(w)}px`,
	});
	// 绘制矩形
	view.drawRect({
		color: styles.background,
		radius: `${uni.upx2px(76)}px`
	}, {
		top: '0px',
		left: '0px',
		width: '100%',
		height: '100%'
	});
	view.draw([{
			tag: 'img',
			id: 'img',
			src: styles.icon,
			position: {
				top: `${uni.upx2px(24)}px`,
				left: `${uni.upx2px(54)}px`,
				width: `${uni.upx2px(60)}px`,
				height: `${uni.upx2px(60)}px`
			}
		},
		{
			tag: 'font',
			id: 'font',
			text: message,
			textStyles: {
				size: `${uni.upx2px(30)}px`,
				color: styles.color,
				align: "center"
			},
			position: {
				left: `${uni.upx2px(40)}px`,
			}
		}
	]);
	view.show();
	setTimeout(() => {
		view.close();
	}, 2000)
}

export default Toast

使用:

Toast({
	message: '取消点赞'
})

小米10真机效果:
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值