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这一开源库实现滑动列表弹出菜单,接 ...
随机推荐
- 关于 webpack 跨域
一.使用 http-proxy-middleware 代理 安装 http-proxy-middleware 依赖 在src 目录下 新建一个 setupProxy.js文件 // 引用依赖 va ...
- <Android 应用 之路> MPAndroidChart~ScatterChart
简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...
- 【JavaScript】闭包应用之数据独立
在平常的开发中,总有一些方法我们在不同的地方都有用的,因此我们会把这些方法封装起来.当我们需要在开发一个功能的时候需要用到一个组合函数(多个函数之间有联系,即有一个或多个共同的全局变量)且这个组合 ...
- The difference between a local variable and a member variable
package com.itheima_04; /* * 成员变量和局部变量的区别: * A:在类中的位置不同 * 成员变量:类中,方法外 * 局部变量:方法中或者方法声明上(形式参数) * B:在内 ...
- Python爬虫教程-06-爬虫实现百度翻译(requests)
使用python爬虫实现百度翻译(requests) python爬虫 上一篇介绍了怎么使用浏览器的[开发者工具]获取请求的[地址.状态.参数]以及使用python爬虫实现百度翻译功能[urllib] ...
- MySQL 练习题2
CREATE TABLE `dept` ( `did` ) NOT NULL AUTO_INCREMENT, `dname` ) DEFAULT NULL, `address` ) DEFAULT N ...
- 用Easing函数实现碰撞效果
用Easing函数实现碰撞效果 工程中需要的源码请从这里下载: https://github.com/YouXianMing/EasingAnimation 源码: // // ViewControl ...
- 初始python(三)
1. 循环 if, while, forbreak : 结束整个循环continue :跳出当前这次循环,但不结束整个循环else :结束整个循环后才执行,不能与break合用,但可以与continu ...
- December 27th 2016 Week 53rd Tuesday
A journey of one thousand miles begins with one step. 千里之行始于足下. No matter how slowly you walk, as lo ...
- ZT 第9章 Framework的启动过程
所在位置: 图书 -> 在线试读 -> Android内核剖析 第9章 Framework的启动过程 9.3 zygote的启动 前面小节介绍了Framework的运行环境,以及Dalvi ...