第二篇、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 预览提交的表单
预览表单,查看后确认提交或者返回重填 演示 XML/HTML Code <form class="mform" id="myform" method=&q ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- ubuntu完美卸载JDK
要删除 OpenJDK (如果已安装的话).首先,检查是安装的哪个 OpenJDK包. # dpkg --list | grep -i jdk 移除 openjdk包: # apt-get purge ...
- XianBicycle
https://github.com/xialinchong/secrettalkandroid https://github.com/talentprince/PhotoView https://g ...
- Android横竖屏切换及其相应布局载入问题
第一.横竖屏切换连带载入多屏布局问题: 假设要让软件在横竖屏之间切换.因为横竖屏的高宽会发生转换,有可能会要求不同的布局. 能够通过下面两种方法来切换布局: 1)在res文件夹下建立layout-la ...
- linux 内核参数图解
https://www.suse.com/documentation/sles11/book_sle_tuning/data/part_tuning_kernel.html http://blog.c ...
- knockout 监控数组的缺点
knockout的监控数组没有想象中的强大,只能监控数组元素的位置变化,或个数的增减,然后对视图对应区域进行同步. <!DOCTYPE html> <html> <hea ...
- C#如何读写和创建INI文件
在做项目过程中,有时需要保存一些简单的配置信息,可以使用xml,也可以使用INI文件.下面是C#中读取INI的方法,相信大部分朋友都使用过这种方式.INI文件的存储方式如下, [section] ke ...
- webrtc学习(二): audio_device之opensles
audio_device是webrtc的音频设备模块. 封装了各个平台的音频设备相关的代码 audio device 在android下封装了两套音频代码. 1. 通过jni调用java的media ...
- LeetCode11 Container With Most Water
题意: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...