第二篇、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 简 ...
随机推荐
- CodeForces 163B Lemmings 二分
Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...
- 架构探险——第二章(为web应用添加业务功能)
第二章不使用框架完成了自己的Web应用. 重点: 服务层的完善优化过程,思路 在看这一段的时候引起了无数次的共鸣.相信大家在开始接触Java Web的时候,都做过类似的封装和优化. 第一版 在Serv ...
- ChRoomtst
https://github.com/JawaJedi/ChRoomtst
- Nice是如何做iOS客户端架构的?
一个创业产品的iOS客户端架构到底怎么做呢?现下最有活力的图片社交软件Nice的技术负责人刘诗彬将为我们解答创业产品如何实现iOS客户端架构. 分享人:刘诗彬,毕业于北京邮电大学电子信息科学与技术专业 ...
- WWH学习模式
WWH是"What+Why+How"的简称,是对学习方法最完美的概括."如果不按照WWH这种模式来教学,90%的结果是老师没教好,学生学不好." 1.What( ...
- Coin Test
描述 As is known to all,if you throw a coin up and let it droped on the desk there are usually three r ...
- jquery datatable[表格处理]
最近在公司实习发现一个额功能强大的表格解决方案,了解了一下,先总结如下: 1.官网:http://www.datatables.net/ 2.需要特别注意:被dataTable处理的table对象,必 ...
- iOS 实现进度条(progress)
#import <UIKit/UIKit.h> @interface ZSDProgressView : UIView { UIView *progressView;//进度view } ...
- 沈逸老师PHP魔鬼特训笔记(1)
此课程个人开发环境可以考虑使用Ubuntu ,推荐sublime和PhpStorm作为开发环境.一.PHP的一大特性是:脚本语言.不要编译,写完就可以运行? 然而并不是....... PHP代码要想运 ...