SoFunction
Updated on 2025-04-04

iOS TabBarItem sets red dot (unread message)

Implementation principle:

In fact, it is to customize a view, add the view to the UITabBar, or it can be a button, set the background image, and label.

Just talk less nonsense and just upload the code

Create a UITabBar category

#import <UIKit/>
@interface UITabBar (Extension)
- (void)showBadgeOnItmIndex:(int)index;
- (void)hideBadgeOnItemIndex:(int)index;
@end
#import "UITabBar+"
#define TabbarItemNums 5.0
@implementation UITabBar (badge)
//Show red dots- (void)showBadgeOnItmIndex:(int)index{
 [self removeBadgeOnItemIndex:index];
 // Create a new little red dot UIView *bview = [[UIView alloc]init];
  = 888+index;
  = 5;
  = YES;
  = [UIColor redColor];
 CGRect tabFram = ;

 float percentX = (index+0.6)/TabbarItemNums;
 CGFloat x = ceilf(percentX*);
 CGFloat y = ceilf(0.1*);
  = CGRectMake(x, y, 10, 10);
 [self addSubview:bview];
 [self bringSubviewToFront:bview];
}
//Hide red dots-(void)hideBadgeOnItemIndex:(int)index{
 [self removeBadgeOnItemIndex:index];
}
//Remove the control- (void)removeBadgeOnItemIndex:(int)index{
 for (UIView*subView in ) {
  if ( == 888+index) {
   [subView removeFromSuperview];
  }
 }
}
@end

Finally, call it in the subcontroller

[ showBadgeOnItmIndex:4];

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!