美文网首页
iOS开发WMPageController自定义角标badge设

iOS开发WMPageController自定义角标badge设

作者: Linda_smile | 来源:发表于2022-04-14 14:54 被阅读0次

    想要实现的效果:


    image.png

    在WMPageController中,其实有角标的设置方法,只是Demo里面并没有完全实现,需要在WMPageController.m方法中实现此代理方法:- (UIView *)menuView:(WMMenuView *)menu badgeViewAtIndex:(NSInteger)index,此方法会返回角标的view。具体实现:

    WMPageController.h
    /*更新角标数据*/
    - (void)updateBadgeWithIndex:(NSInteger)index badgeNum:(NSString *)numStr;
    WMPageController.m
    /*更新角标数据方法实现*/
    - (void)updateBadgeWithIndex:(NSInteger)index badgeNum:(NSString *)numStr
    {
        _badgeIndex = index;
        _badgeNumStr = numStr;
        [self reloadData];
    }
    - (UIView *)menuView:(WMMenuView *)menu badgeViewAtIndex:(NSInteger)index
    {
        if (index == _badgeIndex)
        {
          //计算标题宽度
           CGFloat width =  [_titles[index] workOutSizeWidthWithFontSize:[UIFont systemFontOfSize:_titleSizeNormal] value:[NSValue valueWithCGSize:CGSizeMake(MAXFLOAT, 20)]];
            //计算角标宽度
            CGFloat badgeWidth = [_badgeNumStr workOutSizeWidthWithFontSize:[UIFont systemFontOfSize:9] value:[NSValue valueWithCGSize:CGSizeMake(MAXFLOAT, 20)]];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(([_itemsWidths[index] integerValue] - width) / 2 + width, menu.height / 2 - _titleSizeNormal / 2 - 5, badgeWidth + 10, 10)];
            label.backgroundColor = rgb(234, 51, 35);
            label.layer.cornerRadius = 5;
            label.clipsToBounds = YES;
            label.textColor = [UIColor whiteColor];
            label.text = _badgeNumStr;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:9];
            return label;
        }else
        {
            return  [[UIView alloc] init];
        }
    }
    

    在需要设置角标的位置调用:
    [self upDateTitleWithStr:@"99+" index:1]
    更新角标上的数量。

    相关文章

      网友评论

          本文标题:iOS开发WMPageController自定义角标badge设

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