iOS tabbar 角标

作者: 孤胆走天涯 | 来源:发表于2016-05-25 10:01 被阅读1427次

    #import "UITabBar+badge.h"

    #import "ContantHead.h"

    #define TabbarBadge_Nums 5.0

    @implementation UITabBar (badge)

    //显示小红点

    - (void)showBadgeOnItemIndex:(int)index withNum:(NSInteger)badgeNum{

    //移除之前的小红点

    [self removeBadgeOnItemIndex:index];

    //新建小红点

    UILabel *labNum = [[UILabel alloc]init];

    labNum.tag = 888 + index;

    labNum.layer.masksToBounds = YES;

    labNum.layer.cornerRadius = 20/2;//圆形

    labNum.backgroundColor = [UIColor redColor];//颜色:红色

    CGRect tabFrame = self.frame;

    //确定小红点的位置

    float percentX = (index +0.6) / TabbarBadge_Nums;

    CGFloat x = ceilf(percentX * tabFrame.size.width);

    CGFloat y = ceilf(0.1 * tabFrame.size.height)-5;

    labNum.frame = CGRectMake(x, y, 20, 20);//圆形大小为10

    //-30*screenWidth/375

    labNum.text = [NSString stringWithFormat:@"%ld",badgeNum];

    labNum.textColor = [UIColor whiteColor];

    labNum.adjustsFontSizeToFitWidth = YES;

    labNum.textAlignment = NSTextAlignmentCenter;

    [self addSubview:labNum];

    }

    //隐藏小红点

    - (void)hideBadgeOnItemIndex:(NSInteger)index{

    //移除小红点

    [self removeBadgeOnItemIndex:index];

    }

    //移除小红点

    - (void)removeBadgeOnItemIndex:(NSInteger)index{

    //按照tag值进行移除

    for (UIView *subView in self.subviews) {

    if (subView.tag == 888+index) {

    [subView removeFromSuperview];

    }

    }

    }

    相关文章

      网友评论

      本文标题:iOS tabbar 角标

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