iOS TabBarItem设置红点(未读消息)

实现原理:
其实是自定义一个view,将view添加到UITabBar上面,也可以是一个按钮,设置背景图片,和label。
废话少说直接上代码
搞一个UITabBar的分类
#import <UIKit/UIKit.h> @interface UITabBar (badge) - (void)showBadgeOnItemIndex:(int)index;
- (void)hideBadgeOnItemIndex:(int)index; @end
#import "UITabBar+badge.h" #define TabbarItemNums 5.0 @implementation UITabBar (badge) // 显示红点
- (void)showBadgeOnItemIndex:(int)index { [self removeBadgeOnItemIndex:index];
// 新建小红点
UIView *bview = [[UIView alloc]init];
bview.tag = 888 + index;
bview.layer.cornerRadius = ;
bview.clipsToBounds = YES;
bview.backgroundColor = [UIColor redColor];
CGRect tabFram = self.frame; float percentX = (index+0.6) / TabbarItemNums;
CGFloat x = ceilf(percentX * tabFram.size.width);
CGFloat y = ceilf(0.1 * tabFram.size.height);
bview.frame = CGRectMake(x, y, , );
[self addSubview:bview];
[self bringSubviewToFront:bview];
} // 隐藏红点
- (void)hideBadgeOnItemIndex:(int)index { [self removeBadgeOnItemIndex:index];
}
// 移除控件
- (void)removeBadgeOnItemIndex:(int)index { for (UIView *subView in self.subviews) {
if (subView.tag == 888 + index) {
[subView removeFromSuperview];
}
}
}
@end
最后在子控制器调用就可以啦
#import "UITabBar+badge.h" [self.tabBarController.tabBar showBadgeOnItemIndex:];
swift版带消息个数
import UIKit
extension UITabBar {
func showBadgeOnItem(index:Int, count:Int) {
removeBadgeOnItem(index: index)
let bview = UIView.init()
bview.tag = +index
bview.layer.cornerRadius =
bview.clipsToBounds = true
bview.backgroundColor = UIColor.red
let tabFrame = self.frame
let percentX = (Float(index)+0.6) / 5.0(tabBar的总个数)
let x = CGFloat(ceilf(percentX*Float(tabFrame.width)))
let y = CGFloat(ceilf(0.1*Float(tabFrame.height)))
bview.frame = CGRect(x: x, y: y, width: , height: )
let cLabel = UILabel.init()
cLabel.text = "\(count)"
cLabel.frame = CGRect(x: , y: , width: , height: )
cLabel.font = UIFont.systemFont(ofSize: )
cLabel.textColor = UIColor.white
cLabel.textAlignment = .center
bview.addSubview(cLabel)
addSubview(bview)
bringSubview(toFront: bview)
}
// 隐藏红点
func hideBadgeOnItem(index:Int) {
removeBadgeOnItem(index: index)
}
// 移除控件
func removeBadgeOnItem(index:Int) {
for subView:UIView in subviews {
if subView.tag == +index {
subView.removeFromSuperview()
}
}
}
}
iOS TabBarItem设置红点(未读消息)的更多相关文章
- iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)
一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 //一般view上加角标 _badge1 = [[LFBadge alloc] init ...
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1)
Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1) 现在很多的APP会有新消息/未接来电/未读消息/新通知圆球红点提示,典型的以微信.QQ新消息提示为 ...
- Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒
http://www.51itong.net/android-badgenumber-9789.html Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒 ...
- wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果
上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个 两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...
- 未读消息(小红点),前端与 RabbitMQ实时消息推送实践,贼简单~
前几天粉丝群里有个小伙伴问过:web 页面的未读消息(小红点)怎么实现比较简单,刚好本周手头有类似的开发任务,索性就整理出来供小伙伴们参考,没准哪天就能用得上呢. 之前在 <springboot ...
- 桌面图标未读消息(小米,sony,三星手机)
新消息来了,在桌面的Laucher图标上显示新消息数 /** * 应用桌面图标未读消息显示工具类 * 只支持 小米,三星和索尼 */ public class BadgeUtil { final st ...
- 【Python学习笔记】-APP图标显示未读消息数目
以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...
- Android 高仿QQ滑动弹出菜单标记已读、未读消息
在上一篇博客<Android 高仿微信(QQ)滑动弹出编辑.删除菜单效果,增加下拉刷新功能>里,已经带着大家学习如何使用SwipeMenuListView这一开源库实现滑动列表弹出菜单,接 ...
随机推荐
- 什么是首字节时间(TTFB)
第一字节响应时间(TTFB)=从发送请求到WEB服务器的时间+WEB服务器处理请求并生成响应花费的时间+WEB服务器生成响应到浏览器花费的时间测量第一字节响应时间(TTFB)的工具:http://ww ...
- ssm 注解@ResponseBody 返回json 乱码问题
方法一:在@RequestMapping上加入 produces方法 @RequestMapping(value = "/upload.do",method = RequestMe ...
- Android 性能优化的方面方面都在这儿
又到周六了,鸿洋的不定期的周六放送又来了~~这次来谈谈性能优化吧.大家在工作中或多或少都会拿自家的应用和竞品app做比对,不可避免的需要做一些app性能优化的活.很多时候可能是策略上的调整,不过还是有 ...
- Oracle 给字符串补空格、补0
利用lpad().RPAD()函数来实现给字符串补空格或补0的功能: 一.lpad()lpad函数将左边的字符串填充一些特定的字符其语法格式如下:lpad(string,n,[pad_string]) ...
- Azure 虚拟机如何配置 AntiMalware
本文仅演示通过 PowerShell 方式安装并配置 AntiMalware 的过程. 经典虚拟机 立即访问http://market.azure.cn 加载 Antimalware ...
- CentOS7路由、端口和服务排障常用命令
1. ip route 显示主机基本路由信息 ~]# ip route default via 172.25.0.254 dev eth0 proto static metric 1024 1 ...
- 【Leetcode】【Medium】Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- ESS控制台发布新功能:创建多实例规格的伸缩配置
背景 原弹性伸缩ESS服务限定,生效的伸缩配置中只能对应一种实例规格,这样就会存在如果生效的配置中的实例规格的库存不足(高配实例规格通常更容易出现库存不足的情况)时, 用户配置好的伸缩规则以及伸缩组对 ...
- 沉淀再出发:web服务器和应用服务器之间的区别和联系
沉淀再出发:web服务器和应用服务器之间的区别和联系 一.前言 关于后端,我们一般有三种服务器(当然还有文件服务器等),Web服务器,应用程序服务器和数据库服务器,其中前面两个的概念已经非常模糊了,但 ...
- [BZOJ 2322][BeiJing2011]梦想封印
梦想封印 题意 原题面: Problem 2322. -- [BeiJing2011]梦想封印 2322: [BeiJing2011]梦想封印 Time Limit: 20 Sec Memory L ...