第二篇、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 简 ...
随机推荐
- (转)用AGG实现高质量图形输出(二)
本文上接<用AGG实现高质量图形输出(一)>,分别介绍了AGG显示流程中的各个环节. 上次讲了AGG的显示原理并举了一个简单的例子,这一篇文章开始讲AGG工作流程里的每个环节.为了方便对照 ...
- 关于self.用法的一些总结
转自:http://www.cocoachina.com/bbs/read.php?tid=12850&page=1 最近有人问我关于什么时候用self.赋值的问题, 我总结了一下, 发出来给 ...
- 用JSON数据向已定义列的表格添加数据行
其实添加方式和在MVC中动态读取JSON数据创建表格一样,只不过一个是完整表格添加,一个是从表格中间添加.不详细说明了. <div> <table class="table ...
- sql查询行转列
昨天下午碰到一个需求,一个大约30万行的表,其中有很多重复行,在这些行中某些字段值是不重复的. 比如有ID,NAME,CONTRACT_id,SALES,PRODUCT等,除了PRODUCT字段,其余 ...
- NSNotificationCenter 使用姿势详解
来源:JamesYu 链接:http://www.jianshu.com/p/a4d519e4e0d5 最近在做平板的过程中,发现了一些很不规范的代码.偶然修复支付bug的时候,看到其他项目代码,使用 ...
- C# 模式窗口下更新进度条
public partial class frmProcessBar : Form { public frmProcessBar() { InitializeComponent(); } public ...
- C# mvc--EF中查询的本质
UI层我直接用了窗体程序. 好了 不罗嗦 直接上代码…… private void button1_Click(object sender, EventArgs e) { //1.0创建EF上下文容器 ...
- uiview 的transitionWithView 方法使用
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView transitionWithView:_imageV ...
- c#代码使用ResourceDictionary样式
对于ResourceDictionary样式代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006 ...
- 精通CSS高级Web标准解决方案(1-1选择器)
设计代码的结构 使用有意义的标记 css可以控制页面的外观并将表现与内容分隔开. 在分配ID与类名时尽量保证有意义且与表现无关. div可以对块级元素进行分组,而span可以对行内元素进行分组或标识. ...