当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS TabBarItem设置红点(未读消息)

iOS TabBarItem设置红点(未读消息)

2019年07月24日  | 移动技术网移动技术  | 我要评论

实现原理:

其实是自定义一个view,将view添加到uitabbar上面,也可以是一个按钮,设置背景图片,和label。

废话少说直接上代码

搞一个uitabbar的分类

#import <uikit/uikit.h>
@interface uitabbar (extension)
- (void)showbadgeonitmindex:(int)index;
- (void)hidebadgeonitemindex:(int)index;
@end
#import "uitabbar+extension.h"
#define tabbaritemnums 5.0
@implementation uitabbar (badge)
//显示红点
- (void)showbadgeonitmindex:(int)index{
 [self removebadgeonitemindex:index];
 //新建小红点
 uiview *bview = [[uiview alloc]init];
 bview.tag = 888+index;
 bview.layer.cornerradius = 5;
 bview.clipstobounds = yes;
 bview.backgroundcolor = [uicolor redcolor];
 cgrect tabfram = self.frame;

 float percentx = (index+0.6)/tabbaritemnums;
 cgfloat x = ceilf(percentx*tabfram.size.width);
 cgfloat y = ceilf(0.1*tabfram.size.height);
 bview.frame = cgrectmake(x, y, 10, 10);
 [self addsubview:bview];
 [self bringsubviewtofront:bview];
}
//隐藏红点
-(void)hidebadgeonitemindex:(int)index{
 [self removebadgeonitemindex:index];
}
//移除控件
- (void)removebadgeonitemindex:(int)index{
 for (uiview*subview in self.subviews) {
  if (subview.tag == 888+index) {
   [subview removefromsuperview];
  }
 }
}
@end

最后在子控制器调用就可以啦

[self.tabbarcontroller.tabbar showbadgeonitmindex:4];

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网