【iOS 1 行代码系列】之 一行代码告别系统的大红粗圆点

UITabBar 写个分类,搞定!

上代码:

// UITabBar+JHBadge.h

@interface UITabBar (JHBadge)

/// Show red dot.
- (void)jh_showRedDot:(NSInteger)index;

/// Hide red dot.
- (void)jh_hideRedDot:(NSInteger)index;

@end

// UITabBar+JHBadge.m
#import "UITabBar+JHBadge.h"

@implementation UITabBar (JHBadge)

- (void)jh_showRedDot:(NSInteger)index{
    [[self reddotForIndex:index] setHidden:NO];
}

- (void)jh_hideRedDot:(NSInteger)index{
    [[self reddotForIndex:index] setHidden:YES];
}

#pragma mark - private
- (UIView *)reddotForIndex:(NSInteger)index
{
    NSInteger tag = 666 + index;
    UIView *reddot = [self viewWithTag:tag];
    if (!reddot) {

        // If add custom button in 'Tabbar', 'count' should +1.
        // 如果添加了自定义按钮,这里应该 +1.
        NSInteger count = self.items.count;
        CGFloat X = CGRectGetWidth(self.bounds)*((index+0.65)/count);
        CGFloat Y = CGRectGetHeight(self.bounds)*0.1;
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor redColor];
        view.layer.cornerRadius = 5;
        view.tag = tag;
        view.frame = CGRectMake(X, Y, 10, 10);
        [self addSubview:view];
        reddot = view;
    }
    return reddot;
}

@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,660评论 25 709
  • 无戒365天极限挑战营|第45天 “你给我回来!”随着一声喊叫,一对夫妻在鱼塘边扭打了起来,男的顺势一推,把女的推...
    小凡丫丫阅读 4,697评论 0 2
  • 读《了小狗钱钱》后,我对于收入该怎样分配才有所大致的方向概念。之前,我对于收入就是想花就花,并没有一个限制额度的想...
    谁谁占了我的名儿阅读 1,943评论 0 0
  • iOS-时间与日期详解//www.greatytc.com/p/f8aab37a27ffhttps://g...
    阿龍飛阅读 1,397评论 0 0
  • L,S,W和J正在做老师布置的作业,这时,老师走了进来,对教室里的他们说,“元旦节,你们四个要出一个节目,你们想一...
    蒋莹阅读 4,318评论 0 0