iOS tabbaritem设置红点大小、自定义宽高
来源:互联网 发布:软件测试与年龄 编辑:程序博客网 时间:2024/05/29 08:42
项目需求,希望当用户存在未读消息的时候在对应的UITabBarItem上显示小红点。发现IOS自带的UITabBarItem的badgeValue尺寸偏大,不满足项目需求。
第一步,建一个UITabBar的category类别。
.h文件
#import <UIKit/UIKit.h>
@interface UITabBar (badge)
- (void)showBadgeOnItemIndex:(int)index; //显示小红点
- (void)hideBadgeOnItemIndex:(int)index; //隐藏小红点
@end
.m文件
#import "UITabBar+badge.h"
#define TabbarItemNums 4.0 //tabbar的数量 如果是5个设置为5.0
@implementation UITabBar (badge)
//显示小红点
- (void)showBadgeOnItemIndex:(int)index{
//移除之前的小红点
[self removeBadgeOnItemIndex:index];
//新建小红点
UIView *badgeView = [[UIView alloc]init];
badgeView.tag = 888 + index;
badgeView.layer.cornerRadius = 5;//圆形
badgeView.backgroundColor = [UIColor redColor];//颜色:红色
CGRect tabFrame = self.frame;
//确定小红点的位置
float percentX = (index +0.6) / TabbarItemNums;
CGFloat x = ceilf(percentX * tabFrame.size.width);
CGFloat y = ceilf(0.1 * tabFrame.size.height);
badgeView.frame = CGRectMake(x, y, 10, 10);//圆形大小为10
[self addSubview:badgeView];
}
//隐藏小红点
- (void)hideBadgeOnItemIndex:(int)index{
//移除小红点
[self removeBadgeOnItemIndex:index];
}
//移除小红点
- (void)removeBadgeOnItemIndex:(int)index{
//按照tag值进行移除
for (UIView *subView in self.subviews) {
if (subView.tag == 888+index) {
[subView removeFromSuperview];
}
}
}
@end
第三步,引入到需要使用的类中。
#import "UITabBar+badge.h"
引用代码如下:
//显示
[self.tabBarController.tabBar showBadgeOnItemIndex:2];
//隐藏
[self.tabBarController.tabBar hideBadgeOnItemIndex:2]
大功告成,接下来看看效果。
第一步,建一个UITabBar的category类别。
.h文件
#import <UIKit/UIKit.h>
@interface UITabBar (badge)
- (void)showBadgeOnItemIndex:(int)index; //显示小红点
- (void)hideBadgeOnItemIndex:(int)index; //隐藏小红点
@end
.m文件
#import "UITabBar+badge.h"
#define TabbarItemNums 4.0 //tabbar的数量 如果是5个设置为5.0
@implementation UITabBar (badge)
//显示小红点
- (void)showBadgeOnItemIndex:(int)index{
//移除之前的小红点
[self removeBadgeOnItemIndex:index];
//新建小红点
UIView *badgeView = [[UIView alloc]init];
badgeView.tag = 888 + index;
badgeView.layer.cornerRadius = 5;//圆形
badgeView.backgroundColor = [UIColor redColor];//颜色:红色
CGRect tabFrame = self.frame;
//确定小红点的位置
float percentX = (index +0.6) / TabbarItemNums;
CGFloat x = ceilf(percentX * tabFrame.size.width);
CGFloat y = ceilf(0.1 * tabFrame.size.height);
badgeView.frame = CGRectMake(x, y, 10, 10);//圆形大小为10
[self addSubview:badgeView];
}
//隐藏小红点
- (void)hideBadgeOnItemIndex:(int)index{
//移除小红点
[self removeBadgeOnItemIndex:index];
}
//移除小红点
- (void)removeBadgeOnItemIndex:(int)index{
//按照tag值进行移除
for (UIView *subView in self.subviews) {
if (subView.tag == 888+index) {
[subView removeFromSuperview];
}
}
}
@end
第三步,引入到需要使用的类中。
#import "UITabBar+badge.h"
引用代码如下:
//显示
[self.tabBarController.tabBar showBadgeOnItemIndex:2];
//隐藏
[self.tabBarController.tabBar hideBadgeOnItemIndex:2]
大功告成,接下来看看效果。
0 0
- iOS tabbaritem设置红点大小、自定义宽高
- tabbaritem设置红点大小、自定义宽高
- tabbaritem设置红点大小、自定义宽高
- TabbarItem设置红点大小、自定义宽高
- Tabbaritem设置红点大小、自定义宽高
- tabbaritem设置红点大小、自定义宽高
- tabbaritem设置红点大小、自定义宽高
- tabbaritem设置红点大小、自定义宽高
- iOS开发 UITabBar角标 红点形式 (tabBarItem.badgeValue)
- iOS - 自定义红点
- 自定义TabBarItem的字体颜色和大小
- IOS7 tabBarItem 自定义图片的设置
- IOS7 tabBarItem 自定义图片的设置
- iOS8 TabBarItem设置自定义图片问题
- tabBar中tabBarItem选中颜色自定义设置
- IOS 去掉红点
- 自定义TabBarItem
- iOS - 一个方法搞定自定义tabBarItem中的控件位置均分
- 安装和配置MyEclipse时所遇到的小问题
- 机器人制作入门(二)----给机器一双“眼睛”
- Android Notification常见样式总结
- 数字图像处理(附录1)---概率介绍
- spring-web-4.2.0.RELEASE.jar报错问题
- iOS tabbaritem设置红点大小、自定义宽高
- Leetcode 19 Remove Nth Node from End of List
- Java学习之反射机制浅析(1)
- URLDecoder与URLDecoder的简单了解
- mybatis拦截器
- Android第三方框架universal-image-loader[图片缓存加载]及其OOM问题处理【常用】,CardView使用[给列表中项+边框]
- UIControlEventEditingDidEndOnExit和UIControlEventEditingDidEnd
- 欢迎使用CSDN-markdown编辑器
- 疑惑?实战演示疑惑 mysql insert到底加什么锁