实现原理:

其实是自定义一个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设置红点(未读消息)的更多相关文章

  1. iOS 未读消息角标 仿QQ拖拽 简单灵活 支持xib(源码)

    一.效果 二.简单用法 超级简单,2行代码集成:xib可0代码集成,只需拖一个view关联LFBadge类即可 //一般view上加角标 _badge1 = [[LFBadge alloc] init ...

  2. [iOS微博项目 - 3.6] - 获取未读消息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...

  3. Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1)

     Android BGABadgeView:新消息/未接来电/未读消息/新通知圆球红点提示(1) 现在很多的APP会有新消息/未接来电/未读消息/新通知圆球红点提示,典型的以微信.QQ新消息提示为 ...

  4. Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒

    http://www.51itong.net/android-badgenumber-9789.html Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒 ...

  5. wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果

    上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个  两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...

  6. 未读消息(小红点),前端与 RabbitMQ实时消息推送实践,贼简单~

    前几天粉丝群里有个小伙伴问过:web 页面的未读消息(小红点)怎么实现比较简单,刚好本周手头有类似的开发任务,索性就整理出来供小伙伴们参考,没准哪天就能用得上呢. 之前在 <springboot ...

  7. 桌面图标未读消息(小米,sony,三星手机)

    新消息来了,在桌面的Laucher图标上显示新消息数 /** * 应用桌面图标未读消息显示工具类 * 只支持 小米,三星和索尼 */ public class BadgeUtil { final st ...

  8. 【Python学习笔记】-APP图标显示未读消息数目

    以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...

  9. Android 高仿QQ滑动弹出菜单标记已读、未读消息

    在上一篇博客<Android 高仿微信(QQ)滑动弹出编辑.删除菜单效果,增加下拉刷新功能>里,已经带着大家学习如何使用SwipeMenuListView这一开源库实现滑动列表弹出菜单,接 ...

随机推荐

  1. less教程

    平时在工作中,偶尔会遇到老大让你修改原来写好的样式,如果修改的多的话,修改起来是非常麻烦的.他不像js一样,定义变量.函数,需要修改某些值,直接修改方法就行了.less的出现,恰恰帮我们解决了这个问题 ...

  2. Client tried to access password protected page without proper authorization (status code 401) 无法发布SceneService的解决方法

    前不久,一客户反映原来已经部署好的WebGIS系统忽然无法正常运行了,具体配置如下: Portal for ArcGIS 10.5 ArcGIS Server 10.5 Web Adpator 10. ...

  3. kali 的端口扫描nmap

    输入“nmap+空格+“-O”+空格+IP地址或域名. 扫描所有TCP端口:输入“nmap+空格+“-sT”+空格+IP地址或域名” 扫描所有开放的UDP端口:输入“nmap+空格+”-sP”+空格+ ...

  4. 空间分析开源库GEOS

    History of GEOS GEOS中Geometry的结构 GEOS分析功能简介 1.History of GEOS J​T​S​ ​T​o​p​o​l​o​g​y​ ​S​u​i​t​e是Ma ...

  5. 线性表的Java实现--链式存储(双向链表)

    有了单向链表的基础,双向链表的实现就容易多了. 双向链表的一般情况: 增加节点: 删除节点: 双向链表的Java实现: package com.liuhao.algorithm;      publi ...

  6. netstat统计

    状态统计 netstat -ant | awk '/tcp/ {print $6}'|sort |uniq -c |sort -nr 前十位ESTABLISHED状态ip统计 netstat -ant ...

  7. 关于webWorker的理解和简单例子

    一.理解 当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能.您可以继续做任何愿 ...

  8. Java学习---传输安全设计

    1.计算机安全的概念 用于保护数据和阻止Hacker的工具统称为计算机安全(Computer Security).信息安全最基本的方法就是利用加密信息防止未授权的人窃听,加密是以某种特殊的算法改变原有 ...

  9. iOS自动化-iOS录屏xrecord及解决iPhone设备不显示的问题

    github地址:https://github.com/WPO-Foundation/xrecord 安装方法: git clone https://github.com/WPO-Foundation ...

  10. Spring Boot 的配置文件

    Profile 配置 Profile 是 Spring 用来针对不同的环境对不同的配置提供支持的,全局的 Profile 配置使用 application-{profile}.properties ( ...