美文网首页
tabBar 底部展示红点

tabBar 底部展示红点

作者: super_2e20 | 来源:发表于2019-07-30 10:27 被阅读0次
实现原理:

其实是自定义一个view,将view添加到UITabBar上面,也可以是一个按钮,设置背景图片,和label。
废话少说直接上代码
搞一个UITabBar的分类

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UITabBar (TSTBadge)
- (void)showBadgeOnItemIndex:(int)index;
- (void)hideBadgeOnItemIndex:(int)index;
@end

NS_ASSUME_NONNULL_END

#import "UITabBar+TSTBadge.h"

@implementation UITabBar (TSTBadge)
#define TabbarItemNums 4.0

- (void)showBadgeOnItemIndex:(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];
        break;
        }
    }
}
@end

在首页直接调用

 #import "UITabBar+TSTBadge.h"
[self.tabBarController.tabBar showBadgeOnItemIndex:4];

相关文章

网友评论

      本文标题:tabBar 底部展示红点

      本文链接:https://www.haomeiwen.com/subject/hxdgrctx.html