第二篇、Swift_自定义 tabbar 的 badgeValue显示样式
在实际的开发中,我们常常需要根据实际的需求,去改变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显示样式的更多相关文章
- webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】
自定义webservice服务 我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的 我们发布了we ...
- IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)
********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...
- angularjs学习第三天笔记(过滤器第二篇---filter过滤器及其自定义过滤器)
您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...
- 自定义View5 -塔防小游戏:第二篇防御塔随意放置
第一篇:一个防御塔+多个野怪(简易版) 第二篇:防御塔随意放置 自定义View,处理事件分发,up,move,down. 第三篇:防御塔随意放置+多组野怪 第四篇:多波野怪 第五篇:杀死野怪获得金币 ...
- 1行代码为每个Controller自定义“TabBar”-b
这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...
- 微信小程序自定义 tabbar
一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...
- 【转载】Android Metro风格的Launcher开发系列第二篇
前言: 各位小伙伴们请原谅我隔了这么久才开始写这一系列的第二篇博客,没办法忙新产品发布,好了废话不说了,先回顾一下:在我的上一篇博客Android Metro风格的Launcher开发系列第一篇写了如 ...
- 第二篇 SQL Server安全验证
本篇文章是SQL Server安全系列的第二篇,详细内容请参考原文. 验证是检验主体的过程.主体需要唯一标识,那样SQL Server可以确定主体有哪些权限.正确的验证是提供安全访问数据库对象的必要的 ...
- 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...
随机推荐
- [原创]jQuery的this和$(this)
网上有很多关于jQuery的this和$(this)的介绍,大多数只是理清了this和$(this)的指向,其实它是有应用场所的,不能一概而论在jQuery调用成员函数时,this就是指向dom对象. ...
- SmartThreadPool
https://github.com/amibar/SmartThreadPool 首先是实例化的时候的参数的解释 //Initialize SmartThreadPool & Make lo ...
- java生成汉字验证码
java实现的汉字输入验证码,主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确,实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码图片并用文本框 ...
- MySQL--索引条件下推优化
http://blog.163.com/li_hx/blog/static/1839914132015782821512/ 一 什么是“索引条件下推” “索引条件下推”,称为 Index Condit ...
- char *a 与char a[] 的区别
原文:http://www.cnblogs.com/kaituorensheng/archive/2012/10/23/2736069.html char *a = "hello" ...
- HDU 4605 Magic Ball Game 树状数组
题目大意很简单. 有一颗树(10^5结点),所有结点要么没有子结点,要么有两个子结点.然后每个结点都有一个重量值,根结点是1 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果 ...
- InAction-MR的topK
本来只是想拿搜狗的数据练练手的,却无意踏足MR的topK问题.经过几番波折,虽然现在看起来很简单,但是摸爬滚打中也学到了不少 数据是搜狗实验室下的搜索日志,格式大概为: 00:00:00 298219 ...
- 使用异步httpclient框架做get,post提交数据
1.将异步httpclient框架导入 下载地址:http://download.csdn.net/detail/sinat_32804317/9555641 2.代码实现 public class ...
- C#三大支柱之继承
1.使用base 若子类需要使用父类公开或受保护的成员则需要是base class Manager : Employee { public int StockOptions { get; set; } ...
- 【开源项目12】Retrofit – Java(Android) 的REST 接口封装类库
Retrofit官网:http://square.github.io/retrofit/ Retrofit 和Java领域的ORM概念类似, ORM把结构化数据转换为Java对象,而Retrofit ...