UITabBarItem在iOS 7.1中更改徽章颜色

时间:2014-04-12 10:06:16

标签: ios ios7 uitabbaritem

您好我正在尝试更改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];
                }
            }
        }
    }
}

6 个答案:

答案 0 :(得分:4)

最后,我向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];

答案 1 :(得分:3)

对于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 {}

答案 2 :(得分:3)

试试这个代码iOS 10:

[[[self tabBarController].viewControllers objectAtIndex:1] tabBarItem].badgeColor = [UIColor greenColor];

答案 3 :(得分:1)

您应创建自定义标签栏项类并在那里进行自定义。您可以找到许多用于创建自定义标签栏的文章/代码。

示例:How to create a fully customized tab bar in your iOS App

否则,您将不得不创建可能会破坏或不起作用的变通方法和黑客攻击。

答案 4 :(得分:0)

在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"

答案 5 :(得分:0)

使用此代码:

-(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];