实现原理:

其实是自定义一个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. ArcGISPlotSilverlightAPI For WPF

    这两天有个需求,在地图上做标绘箭头,效果如下图. Arcgis for WPF 10.2.5.0版本,然而官方文档中没有这种API,自己去写一个呢,又感觉无从下手.无奈去网上搜索了一下,发现一篇好文: ...

  2. [转载]一步一步教你如何在Virtualbox虚拟机中安装Remix

    原文地址:https://bbs.jide.com/forum.php?mod=viewthread&tid=4892 大神请路过-- [准备工具] 1.Virtualbox虚拟机(这个是免费 ...

  3. 利用dotnet restore 导入本地 .nupkg 包

    dotnet restore 主要是用于部署.net core 项目中所需的依赖库,集成了nuget包管理软件.因此,dotnet restore 实际上就是根据project.json(今后可能为p ...

  4. 基于TCP/IP的程序设计

    TCP特点 (1)面向连接的传输 (2)端到端的通信 (3)高可靠性,确保传输数据的正确性,不会出现丢失或者乱序 (4)全双工方式传输 (5)采用字节流方式,以字节为单位传输字节序列 (6)紧急数据传 ...

  5. leetcode Ch5-Linked List

    一. 1. Remove Duplicates from Sorted List II class Solution { public: ListNode* deleteDuplicates(List ...

  6. delete in javascript

    Key word delete. 1. Delete global object. x = 42; // creates the property x on the global object var ...

  7. [EffectiveC++]item03:尽可能使用const 并且转载一篇关于const函数的博客

    速度 #include <iostream> using namespace std; class TextBlock { private: string text; public: Te ...

  8. SVG中的元素属性

    SVG attributes by category Animation event attributes onbegin, onend, onload, onrepeat Animation att ...

  9. less使用总结

    15年自学了 less ,可是一直没用,就忘记了.后来抱着提高 css 开发速度的目的,又去学习了 less ,学完马上用,效果立竿见影,记得也牢了.刚开始学习前,我们总会问自己一个问题,学习它有什么 ...

  10. canvas图形库

    总结了一些canvas绘制2d图形的方法,记录在博客中,以便需要的同学参考,也便于日后加深记忆. 1. 圆角矩形: 如上图:w表示矩形的宽,h表示矩形的高,r表示矩形圆角的半径.整个矩形在画布中,(0 ...