美文网首页iOS-1 行代码系列
【iOS 1 行代码系列】之 一行代码告别系统的大红粗圆点

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

作者: 豪冷 | 来源:发表于2018-05-22 08:50 被阅读17次

    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

    相关文章

      网友评论

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

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