2

I have a custom category on UITabBarItem that lets me customize the badge properties. All my code related to the functionality of that is working fine.

However, UIAppearance is not working.

My methods never get called and if I add [[UITabBarItem appearance] customBadgeBackgroundColor] to my appDelegate it crashes with an NSInvalidArgument error deep in the iOS SDK. If I add this method later in my app nothing happens and the invocations for UITabBarItem are null.

setCustomBadgeBackgroundColor: is never called.

I have attached the relevant and minimal code for this. Any help would be greatly appreciated. Thanks!

UITabBarItem+CustomBadge.h

#import <UIKit/UIKit.h>

@interface UITabBarItem (CustomBadge)

@property (nonatomic, assign) UIColor *customBadgeBackgroundColor UI_APPEARANCE_SELECTOR; UIAppearance doesn't work

@end

UITabBarItem+CustomBadge.m

#import "UITabBarItem+CustomBadge.h"
#import <objc/runtime.h>

@implementation UITabBarItem (CustomBadge)

- (UIColor *)customBadgeBackgroundColor
{
    UIColor *customBadgeBackgroundColor = objc_getAssociatedObject(self, @selector(customBadgeBackgroundColor));
    if (!customBadgeBackgroundColor) {
        return [UIColor redColor];
    } else {
        return customBadgeBackgroundColor;
    }
}

- (void)setCustomBadgeBackgroundColor:(UIColor *)customBadgeBackgroundColor
{
    objc_setAssociatedObject(self, @selector(customBadgeBackgroundColor), customBadgeBackgroundColor, OBJC_ASSOCIATION_RETAIN);

    UILabel *customBadgeLabel = objc_getAssociatedObject(self, @selector(customBadgeValue));
    customBadgeLabel.backgroundColor = customBadgeBackgroundColor;
}

@end

Full source code can be found here. https://github.com/valleyman86/UITabBarItem-CustomBadge

MurderDev
  • 157
  • 1
  • 10

0 Answers0