Skip to content

ratulSharker/UITabbarItem-CustomBadge

Repository files navigation



codebeat badge Build Status Cocoapod Platform License

Demo

Background

There is no public api from Apple. But it's not impossible to cusotmize the UITabbarItem badge with UILabel. I've been inspired by enryold's repository. I think this could be done in a better way, by overriding the -(void)setBadgeValue:(NSString*)value & the -(NSString*)badgeValue. In this way an existing project doesn't need to change the badge value settings related function calls. This is how the whole thing is maintained.

This project is under the MIT Liecense, feel free to use this code base under compilance.

Installation

Using Cocoapod

pod 'UITabbarItem-CustomBadge'

Manual Installation

Installation of UITabbarItem+CustomBadge is easy, just add UITabbarItem+CustomBadge category in your xcode project. Change the content of this category to meet your requiremnt and you're good to go.

Installation of UITabbarItem+CustomBadge is easy, include following files in your project

Initializing

Now initilaize custom badge in your project's AppDelegate's didFinishLaunchingwithOptions... as follows

//other imports...
#import "UITabbarItem+CustomBadge.h"
#import "DefaultTabbarBadgeAnimation.h"
#import "DefaultSystemLikeBadgeConfiguration.h"


...
...
...
- (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;
}

Advance Customization

Customizing the badge appearance

To change a new appearance for the badge, create a new class extending NSObject conforming the protocol UITabbarItemBadgeConfiguration.h. Declare all the properties which are in the protocol. In the implementation file set your appropriate values to meet your requirement. Then to use this configuration implementation set as follows :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
      //
      // here assumed that the MyOwnConfiguration is the class that you made for configuration
      //
      //supplying the animation parameter
      [UITabBarItem setDefaultAnimationProvider:[[DefaultTabbarBadgeAnimation alloc] init]];
      [UITabBarItem setDefaultConfigurationProvider:[[MyOwnConfiguration alloc] init]];
            
      //rest of your code goes following...
            
      return YES;
}

Customizing the animation style

In order to provide your own animation, declare a new class extends from NSObject which conforms to the protocol UITabbarItemAnimation.h. Implement two methods of the protocol. Then to use this configuration implementation set as follows :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
      //
      // here assumed that the MyOwnAnimationProvider is the class that you made for configuration
      //
      //supplying the animation parameter
      [UITabBarItem setDefaultAnimationProvider:[[MyOwnAnimationProvider alloc] init]];
      [UITabBarItem setDefaultConfigurationProvider:[[DefaultSystemLikeBadgeConfiguration alloc] init]];
            
      //rest of your code goes following...
            
      return YES;
}

Feel free to file an issue if there any.

Changelogs

Change log 2.0.3

  1. version 2.0.2 was pointing in the wrong tag 2.0.1, thats why cocoapod was installing previous version files. Cocoapod doesn't support resubmission of same version, thats why a new version is needed to introduced to resolve the problem.

Change log 2.0.2

  1. Two new animation class are added as the default animation class.
  2. Example project updated to show demonstration of custom animation classes.
  3. Two new animation uses UIViewAnimationOptionAllowUserInteraction which is available ios8.0+ so the project min sdk version changes to ios8.0, if you want to support lower version of SDK please ignore these two animation classes.
  4. Project builded with xcode8.2.1 .