想要实现的效果:
在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]
更新角标上的数量。