当前位置:首页 > 通信资讯 > 正文

实现原理:

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

废话少说直接上代码

搞一个UITabBar的分类

?
1 2 3 4 5 #import <UIKit/UIKit.h> @interface UITabBar (Extension) - (void)showBadgeOnItmIndex:(int)index; - (void)hideBadgeOnItemIndex:(int)index; @end
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #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];

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

原文链接:http://www.jianshu.com/p/8d654aa2aac3

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。