Is it possible to change UITabBarItem badge color

前端 未结 14 1630
别那么骄傲
别那么骄傲 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 01:05

    // change TabBar BadgeView background Color
    -(void)changeTabBarBadgeViewBgColor:(UITabBar*)tabBar {
        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 {
                                [badgeSubview setValue:nil forKey:@"image"];
                                [badgeSubview setBackgroundColor:[UIColor blueColor]];
                                badgeSubview.clipsToBounds = YES;
                                badgeSubview.layer.cornerRadius = badgeSubview.frame.size.height/2;
                            }
                            @catch (NSException *exception) {}
                        }
    
                        if ([badgeSubview isKindOfClass:[UILabel class]]) {
                            ((UILabel *)badgeSubview).textColor = [UIColor greenColor];
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题