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

上代码

// 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

猜你喜欢

转载自blog.csdn.net/xjh093/article/details/79917741