iOS中TabBarItem设置红点的示例分析

发布时间:2021-07-08 18:18:58 作者:小新
来源:亿速云 阅读:380

这篇文章将为大家详细讲解有关iOS中TabBarItem设置红点的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

实现原理:

其实是自定义一个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];

关于“iOS中TabBarItem设置红点的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. iOS APNs的示例分析
  2. iOS中不该设置self.view属性的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:CSS3如何仿微信实现聊天小气泡

下一篇:ViewPager+RadioGroup如何仿微信实现主界面

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》