UITabBar類別.h文件
#import <UIKit/UIKit.h>@interface UITabBar (Badge)- (void)showBadgeOnItemIndex:(int)index; //顯示小紅點- (void)hideBadgeOnItemIndex:(int)index; //隱藏小紅點@endUITabBar類別.m文件
#import "UITabBar+Badge.h"#define TABBARCount 4 //tabbar個數@implementation UITabBar (Badge)//顯示小紅點- (void)showBadgeOnItemIndex:(int)index{ //移除之前的小紅點 [self removeBadgeOnItemIndex:index]; //新建小紅點 UIView *badgeView = [[UIView alloc]init]; badgeView.tag = 888 + index; badgeView.layer.cornerRadius = 5;//圓形 badgeView.backgroundColor = [UIColor redColor];//顏色:紅色 CGRect tabFrame = self.frame; //確定小紅點的位置 float percentX = (index +0.7)/TABBARCount ; CGFloat x = ceilf(percentX * tabFrame.size.width); CGFloat y = ceilf(0.1 * tabFrame.size.height); badgeView.frame = CGRectMake(x, y, 10, 10);//圓形大小為10 [self addSubview:badgeView];}//隱藏小紅點- (void)hideBadgeOnItemIndex:(int)index{ //移除小紅點 [self removeBadgeOnItemIndex:index];}//移除小紅點- (void)removeBadgeOnItemIndex:(int)index{ //按照tag值進行移除 for (UIView *subView in self.subviews) { if (subView.tag == 888+index) { [subView removeFromSuperview]; } }}@end該內容都是在網上找資料再根據自身需求修改的,若有侵權,請聯系我!
新聞熱點
疑難解答