iOS 7.1 中的 UITabBarItem 更改徽章颜色
Posted
技术标签:
【中文标题】iOS 7.1 中的 UITabBarItem 更改徽章颜色【英文标题】:UITabBarItem change badge colour in iOS 7.1 【发布时间】:2014-04-12 10:06:15 【问题描述】:您好,我正在尝试更改 UITabBarItem 的徽章颜色。下面是我用来实现相同目的的代码。以下代码适用于 ios 7.0。但它不适用于 iOS 7.1。任何帮助表示赞赏。
for (UIView* tabBarButton in _tabbar.subviews)
for (UIView* badgeView in tabBarButton.subviews)
NSString* className = NSStringFromClass([badgeView class]);
// looking for _UIBadgeView
if ([className rangeOfString:@"BadgeView"].location != NSNotFound)
for (UIView* badgeSubview in badgeView.subviews)
NSString* className = NSStringFromClass([badgeSubview class]);
// looking for _UIBadgeBackground
if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound)
@try
NSLog(@"*******************BADGE IMAGE SET *******************");
[badgeSubview setValue:[UIImage imageNamed:@"count_bg.png"] forKey:@"image"];
//[badgeSubview setTintColor:[UIColor greenColor]];
@catch (NSException *exception)
if ([badgeSubview isKindOfClass:[UILabel class]])
((UILabel *)badgeSubview).textColor = [UIColor whiteColor];
【问题讨论】:
【参考方案1】:最后,我向 tabBar 添加了一个带有所需颜色的标签,并得到了预期的结果。
UILabel *badge=[[UILabel alloc]init];
badge.text = @"2";
badge.textAlignment=NSTextAlignmentCenter;
badge.frame=CGRectMake(122, 1, 20, 20);
badge.layer.cornerRadius=10;
badge.textColor=[UIColor whiteColor];
badge.backgroundColor=[UIColor greenColor];
[tabbar addSubview:badge];
【讨论】:
【参考方案2】:对于 Swift,它适用于任何屏幕尺寸和任何方向。
extension UITabBarController
func setBadges(badgeValues: [Int])
for view in self.tabBar.subviews
if view is CustomTabBadge
view.removeFromSuperview()
for index in 0...badgeValues.count-1
if badgeValues[index] != 0
addBadge(index, value: badgeValues[index], color:UIColor(paletteItem: .Accent), font: UIFont(name: Constants.ThemeApp.regularFontName, size: 11)!)
func addBadge(index: Int, value: Int, color: UIColor, font: UIFont)
let badgeView = CustomTabBadge()
badgeView.clipsToBounds = true
badgeView.textColor = UIColor.whiteColor()
badgeView.textAlignment = .Center
badgeView.font = font
badgeView.text = String(value)
badgeView.backgroundColor = color
badgeView.tag = index
tabBar.addSubview(badgeView)
self.positionBadges()
override public func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
self.tabBar.setNeedsLayout()
self.tabBar.layoutIfNeeded()
self.positionBadges()
// Positioning
func positionBadges()
var tabbarButtons = self.tabBar.subviews.filter (view: UIView) -> Bool in
return view.userInteractionEnabled // only UITabBarButton are userInteractionEnabled
tabbarButtons = tabbarButtons.sort( $0.frame.origin.x < $1.frame.origin.x )
for view in self.tabBar.subviews
if view is CustomTabBadge
let badgeView = view as! CustomTabBadge
self.positionBadge(badgeView, items:tabbarButtons, index: badgeView.tag)
func positionBadge(badgeView: UIView, items: [UIView], index: Int)
let itemView = items[index]
let center = itemView.center
let xOffset: CGFloat = 12
let yOffset: CGFloat = -14
badgeView.frame.size = CGSizeMake(17, 17)
badgeView.center = CGPointMake(center.x + xOffset, center.y + yOffset)
badgeView.layer.cornerRadius = badgeView.bounds.width/2
tabBar.bringSubviewToFront(badgeView)
class CustomTabBadge: UILabel
【讨论】:
代码有效,但您必须按位置对 tabbarItems 进行排序:tabbarButtons.sort( $0.frame.origin.x 您有问题吗?因为它们总是根据我的经验默认排序。 是的,它们的顺序与正常顺序不同,可能是 Swift 或 Xcode 的最新内容。【参考方案3】:试试这个代码 iOS 10:
[[[self tabBarController].viewControllers objectAtIndex:1] tabBarItem].badgeColor = [UIColor greenColor];
【讨论】:
【参考方案4】:您应该创建一个自定义标签栏项目类并在那里进行自定义。您可以找到许多用于创建自定义标签栏的文章/代码。
示例:How to create a fully customized tab bar in your iOS App
否则,您将不得不创建可能会破坏或不起作用的变通方法和技巧。
【讨论】:
【参考方案5】:在 Swift 语言中,我使用了下面的代码,感谢 CrazyDeveloper
var tbc: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
self.window?.rootViewController = tbc
var array = tbc.tabBar.subviews as Array
if lblNotificationBadge == nil
lblNotificationBadge = UILabel(frame: CGRectMake(36, 2, 15, 15))
lblNotificationBadge!.textAlignment = NSTextAlignment.Center
lblNotificationBadge!.font = UIFont(name:"Montserrat-Light", size:10)
lblNotificationBadge!.textColor = UIColor.whiteColor()
lblNotificationBadge!.backgroundColor = UIColor(red: 30/255, green: 177/255, blue: 71/255, alpha: 1)
lblNotificationBadge!.layer.cornerRadius = 7
lblNotificationBadge!.clipsToBounds = true
array[2].addSubview(lblNotificationBadge!)
lblNotificationBadge!.text = "4"
【讨论】:
【参考方案6】:使用此代码:
-(void) setBadge : (int) itemCount : (UITabBarController *) tabBarController
// first removing the prevues badge if added
NSArray *viewsToRemove = [tabBarController.tabBar subviews];
for (UIView *v in viewsToRemove)
if(v.tag == 999)
[v removeFromSuperview];
// now if count is not equal to 0 adding a new badge to your tabbar
if(itemCount != 0)
UILabel *badge = [UILabel new];
badge.text = [NSString stringWithFormat:@"%i" , itemCount];
badge.font = [UIFont fontWithName:@"Custom font" size:10];
badge.textAlignment=NSTextAlignmentCenter;
badge.frame=CGRectMake(([[UIScreen mainScreen] bounds].size.width/2), 4, 16 , 16);
badge.textColor=[UIColor whiteColor];
badge.backgroundColor = [UIColor redColor];;
badge.layer.cornerRadius=8;
badge.clipsToBounds = YES;
badge.tag = 999;
[tabBarController.tabBar addSubview:badge];
然后像这样轻松地调用它:
[self setBadge:1 :self.tabBarController];
【讨论】:
以上是关于iOS 7.1 中的 UITabBarItem 更改徽章颜色的主要内容,如果未能解决你的问题,请参考以下文章
iOS 11 中的横向 UITabBar 中的 UITabBarItem
在 iOS 9 中更改 UITabBarItem 色调颜色?
如何将 UITabBarItem 标题更改为另一个 UITabBarItem 的标题?