更改UITabBar徽标颜色

4

是否有可能更改使用Xcode构建的Objective-C iPhone应用程序的选项卡栏徽章颜色?它总是呈现为红色。请问有人能够指导我如何解决这个问题吗?

谢谢。


link - Mathi Arasan
4个回答

5
在iOS 10及以后的版本中,设置徽章颜色最简单的方法是:
if #available(iOS 10, *) 
{
    UITabBarItem.appearance().badgeColor = .green
}

4

针对使用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 < $1.frame.origin.x }) - agfa555

2

好像苹果看到了这个需求,在iOS 10中你可以使用UITabBarItem的UIColor *badgeColor属性。


它可以工作,但请记得为那些使用iOS 9及以前版本的用户提供向后兼容性。 - Yao Li

-1
一个更强大的解决方案在这里UITabbarItem-CustomBadge

演示

enter image description here

这两行代码可以让你开始。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  //supplying the animation parameter
  [UITabBarItem setDefaultAnimationProvider:[[DefaultTabbarBadgeAnimation alloc] init]];
  [UITabBarItem setDefaultConfigurationProvider:[[DefaultSystemLikeBadgeConfiguration alloc] init]];

  //rest of your code goes following...

  return YES;
}

在放置-1之前,请放置一条注释,解释答案的问题。 - Ratul Sharker
1
我已经取消了对这个回答和另一个回答的踩。你最初的回答只是一个指向你的存储库的链接,没有演示,这就是为什么你被踩了。 - user247702

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接