在实际的开发中,我们常常需要根据实际的需求,去改变bageValue的显示样式,默认是红色的背景,白色的字体颜色

使用方式:

class BKTabBarController: UITabBarController {

    override func viewDidLoad() {
super.viewDidLoad() self.addCustomTabbar() } func addCustomTabbar() { let newTabbar = BKTabbar(frame: self.tabBar.frame)
newTabbar.items = self.tabBar.items
newTabbar.badgeValueTextColor = UIColor.black
newTabbar.badgeValueBackgroundColor = UIColor.yellow
self.setValue(newTabbar, forKey: "tabBar") } }

设置数值的方式:

self.tabBarItem.badgeValue = ""

通过 UIViewControler 的 tabBarItem.badgeValue 属性设置 badgeValue的值 , 跟系统的 badgeValue 设置方式一致,只是屏蔽了系统的实现方法,改成自定义 badgeValue 的显示样式

0.UITabBarItem+Extension

#import <UIKit/UIKit.h>

@interface UITabBarItem (Extension)

/**
* tabBarItem 的 tag
*/
@property (nonatomic,assign) NSInteger index; /**
* 自定义 tabBar 的 badgeValue
*/
@property (nonatomic,copy,nullable) NSString *customBadgeValue; @end
#import "UITabBarItem+Extension.h"
#import <objc/runtime.h> static NSString *const kItemTagKey = @"tags"; static NSString *const kItemBadgeValue = @"badgeValue"; @implementation UITabBarItem (Extension) - (void)setIndex:(NSInteger)index { NSNumber *number = [NSNumber numberWithInteger:index];
objc_setAssociatedObject(self, &kItemTagKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (NSInteger)index { NSNumber *number = (NSNumber *)objc_getAssociatedObject(self, &kItemTagKey);
return number.integerValue; } - (void)setCustomBadgeValue:(NSString *)customBadgeValue { objc_setAssociatedObject(self, &kItemBadgeValue, customBadgeValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [[NSNotificationCenter defaultCenter] postNotificationName:@"TabbarBadgeValueChange" object:self]; } - (NSString *)customBadgeValue { NSString *value = (NSString *)objc_getAssociatedObject(self, &kItemBadgeValue); return value;
} - (void)setBadgeValue:(NSString *)badgeValue { self.customBadgeValue = badgeValue;
} @end

1.自定义的tabbar

#import <UIKit/UIKit.h>

/**
* 自定义 tabBar 解决了 badgeValue 显示问题
*/
@interface BKTabbar : UITabBar /**
* 自定义 badgeValue 的背景色
*/
@property (nonatomic,strong,nullable) UIColor *badgeValueBackgroundColor;
/**
* 自定义 badgeValue 的文字颜色
*/
@property (nonatomic,strong,nullable) UIColor *badgeValueTextColor; @end
#import "BKTabbar.h"
#import "UITabBarItem+Extension.h" @interface BKTabbar () @property (nonatomic,strong,nullable) NSMutableDictionary *badgeValuesDic; @end @implementation BKTabbar - (NSMutableDictionary *)badgeValuesDic {
if (!_badgeValuesDic) {
_badgeValuesDic = [NSMutableDictionary dictionary];
}
return _badgeValuesDic;
} - (void)layoutSubviews { [super layoutSubviews]; NSInteger indexTag = ;
NSMutableArray *tempArray = [NSMutableArray array]; // 便利 tabbar 所有子视图
for (UIView *sub in self.subviews) { if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) { if ([tempArray containsObject:sub]) continue;
[tempArray addObject:sub]; }
}
// 临时数组保存四个按钮
[tempArray sortUsingComparator:^NSComparisonResult(UIButton *btn1, UIButton *btn2) { NSNumber *number1 = [NSNumber numberWithFloat:btn1.frame.origin.x];
NSNumber *number2 = [NSNumber numberWithFloat:btn2.frame.origin.x]; return [number1 compare :number2]; }]; // 便利数组给 badgeValue 赋值
for (UIButton *button in tempArray) { NSString *key = [NSString stringWithFormat:@"%@",@(indexTag)];
CGFloat buttonWidth = button.frame.size.width;
UILabel *oldLabel = [button viewWithTag:key.integerValue]; // 先找重用 不存在就创建一个
if (!oldLabel) { UILabel *label = [[UILabel alloc] init];
label.tag = indexTag;
label.textColor = self.badgeValueTextColor;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13.0f];
label.backgroundColor = self.badgeValueBackgroundColor;
label.layer.masksToBounds = YES;
[button addSubview:label];
oldLabel = label; } oldLabel.hidden = YES;
UITabBarItem *currentItem = self.badgeValuesDic[key];
indexTag++; if (!currentItem) continue;
if (currentItem.customBadgeValue == nil) continue;
if ([currentItem.customBadgeValue isEqualToString:@""]) return;
oldLabel.hidden = NO; NSString *title = currentItem.customBadgeValue; CGFloat itemWidth = 0.0f;
CGFloat cornerRadius = 0.0f;
NSInteger textLength = title.length > ? : title.length;
itemWidth = 18.0f + (textLength-) *8.0f;
cornerRadius = 9.0f; oldLabel.frame = CGRectMake(buttonWidth *0.58f, 2.0f, itemWidth, 18.0f);
oldLabel.text = title;
oldLabel.layer.cornerRadius = cornerRadius; } } - (void)setItems:(NSArray<UITabBarItem *> *)items { [super setItems:items]; NSInteger index = ; for (UITabBarItem *item in self.items ) { item.index = index; [self.badgeValuesDic setValue:item forKey:[NSString stringWithFormat:@"%ld",(long)item.index]]; index ++;
} } - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(badgeValueDidChange:) name:@"TabbarBadgeValueChange" object:nil];
self.badgeValueBackgroundColor = [UIColor redColor];
self.badgeValueTextColor = [UIColor redColor]; }
return self;
} #pragma mark - tabbar的 badgeValue 发生改变的通知 - (void)badgeValueDidChange:(NSNotification *)noti { UITabBarItem *item = (UITabBarItem *)noti.object; if (!item || item.index == ) return; NSString *key = [NSString stringWithFormat:@"%ld",(long)item.index]; self.badgeValuesDic[key] = item; [self setNeedsLayout]; } @end

第二篇、Swift_自定义 tabbar 的 badgeValue显示样式的更多相关文章

  1. webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】

    自定义webservice服务 我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的 我们发布了we ...

  2. IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  3. angularjs学习第三天笔记(过滤器第二篇---filter过滤器及其自定义过滤器)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  4. 自定义View5 -塔防小游戏:第二篇防御塔随意放置

    第一篇:一个防御塔+多个野怪(简易版) 第二篇:防御塔随意放置 自定义View,处理事件分发,up,move,down. 第三篇:防御塔随意放置+多组野怪 第四篇:多波野怪 第五篇:杀死野怪获得金币 ...

  5. 1行代码为每个Controller自定义“TabBar”-b

    这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...

  6. 微信小程序自定义 tabbar

    一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...

  7. 【转载】Android Metro风格的Launcher开发系列第二篇

    前言: 各位小伙伴们请原谅我隔了这么久才开始写这一系列的第二篇博客,没办法忙新产品发布,好了废话不说了,先回顾一下:在我的上一篇博客Android Metro风格的Launcher开发系列第一篇写了如 ...

  8. 第二篇 SQL Server安全验证

    本篇文章是SQL Server安全系列的第二篇,详细内容请参考原文. 验证是检验主体的过程.主体需要唯一标识,那样SQL Server可以确定主体有哪些权限.正确的验证是提供安全访问数据库对象的必要的 ...

  9. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究

      关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...

随机推荐

  1. Codeforces Round #340 (Div. 2) B. Chocolate 水题

    B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...

  2. CodeForces 173B Chamber of Secrets spfa

    Chamber of Secrets 题目连接: http://codeforces.com/problemset/problem/173/B Description "The Chambe ...

  3. BIOS 深入学习 转

    http://blog.csdn.net/lightseed/article/category/547391

  4. 解决TIME_WAIT过多问题

    http://bbs.linuxtone.org/thread-853-1-1.html http://bbs.linuxtone.org/thread-2327-1-1.html

  5. 实例讲解Linux系统中硬链接与软链接的创建

    导读 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接.硬链接与软链接的区别从根本上要从Inode节点说 ...

  6. 关于apache的重启

    一:Apache重起的最好方法apachectl graceful http://blog.csdn.net/qianling3439/article/details/8622618 二:请问apac ...

  7. SQL(基于MySQL)——LIMIT用法

    mysql支持limitselect * from tablename limit n,mn:表示从第几行开始,mysql的行数是从0开始标注. m:表示要显示几行: 例如:select * from ...

  8. kafka的一些名词

    broker.id 区kafka集群中每台机器的标识 log.dirs 日志的存放目录,这个最好不要放到/tmp目录下,因为kafka的已被消费和未被消费的数据也被当成“日志”存放到了日志目录,: l ...

  9. IIS 之 打开/关闭 Internet 信息服务

    由于建立测试网站测试代码,重装电脑后不知道IIS在哪打开.下面以windows7为例介绍,打开IIS管理器的简要步骤. 第一步.查找IIS 1.点击" 开始 "→" 控制 ...

  10. CALayer实现遮罩效果

    #import "ViewController.h" @interface ViewController () @property(nonatomic,strong)CALayer ...