Is it possible to change UITabBarItem badge color

前端 未结 14 1486
别那么骄傲
别那么骄傲 2020-12-05 00:14

I want to change background color of UITabBarItem badge but can\'t find any resource on how to make it.

相关标签:
14条回答
  • 2020-12-05 00:58

    I have the same problem and solved it by creating a little category that replace the BadgeView with an UILabel that you can customize easily.

    https://github.com/enryold/UITabBarItem-CustomBadge/

    0 讨论(0)
  • 2020-12-05 01:00

    For people using Swift, I managed to improve on TimWhiting answer in order to have the badge view working on any screen size and any orientation.

     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.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 {}
    
    0 讨论(0)
  • 2020-12-05 01:03

    No you can't change the color but you can use your own badges instead. Add this extension at the file scope and you can customise the badges however you like. Just call self.tabBarController!.setBadges([1,0,2]) in any of your root view controllers.

    To be clear that is for a tab bar with three items, with the badge values going from left to right.

    extension UITabBarController {
        func setBadges(badgeValues:[Int]){
    
            var labelExistsForIndex = [Bool]()
    
            for value in badgeValues {
                labelExistsForIndex.append(false)
            }
    
            for view in self.tabBar.subviews {
                if view.isKindOfClass(PGTabBadge) {
                    let badgeView = view as! PGTabBadge
                    let index = badgeView.tag
    
                    if badgeValues[index]==0 {
                        badgeView.removeFromSuperview()
                    }
    
                    labelExistsForIndex[index]=true
                    badgeView.text = String(badgeValues[index])
    
                }
            }
    
            for var i=0;i<labelExistsForIndex.count;i++ {
                if labelExistsForIndex[i] == false {
                    if badgeValues[i] > 0 {
                        addBadge(i, value: badgeValues[i], color:UIColor(red: 4/255, green: 110/255, blue: 188/255, alpha: 1), font: UIFont(name: "Helvetica-Light", size: 11)!)
                    }
                }
            }
    
    
        }
    
        func addBadge(index:Int,value:Int, color:UIColor, font:UIFont){
    
            let itemPosition = CGFloat(index+1)
            let itemWidth:CGFloat = tabBar.frame.width / CGFloat(tabBar.items!.count)
    
            let bgColor = color
    
            let xOffset:CGFloat = 12
            let yOffset:CGFloat = -9
    
            var badgeView = PGTabBadge()
            badgeView.frame.size=CGSizeMake(17, 17)
            badgeView.center=CGPointMake((itemWidth * itemPosition)-(itemWidth/2)+xOffset, 20+yOffset)
            badgeView.layer.cornerRadius=badgeView.bounds.width/2
            badgeView.clipsToBounds=true
            badgeView.textColor=UIColor.whiteColor()
            badgeView.textAlignment = .Center
            badgeView.font = font
            badgeView.text = String(value)
            badgeView.backgroundColor = bgColor
            badgeView.tag=index
            tabBar.addSubview(badgeView)
    
        }
    }
    
    class PGTabBadge: UILabel {
    
    }
    
    0 讨论(0)
  • 2020-12-05 01:03

    It appears that no. You may only set the value. From Apple's documentation badge is:

    Text that is displayed in the upper-right corner of the item with a surrounding red oval.

    0 讨论(0)
  • 2020-12-05 01:03

    Take a look here @ UITabbarItem-CustomBadge.

    A complete demonstration is following

    it takes only two line of code, if you want to use the default implementation

    - (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;
    }
    
    0 讨论(0)
  • 2020-12-05 01:04

    You can now do it in the storyboard too, by selecting your tab bar item and going to the attributes inspector.

    0 讨论(0)
提交回复
热议问题