TabBar添加 小紅點 (不顯示未讀消息數(shù)量)只是 紅點

轉(zhuǎn)自:http://blog.csdn.net/u013531246/article/details/44460115

為 Tabbar 添加 類別 : HTBadge

.h文件

import <UIKit/UIKit.h>

@interface UITabBar (badge)

/顯示小紅點/

  • (void)showBadgeOnItemIndex:(int)index;
    /隱藏小紅點/
  • (void)hideBadgeOnItemIndex:(int)index;

@end

.m文件

import "UITabBar+badge.h"

define TabbarItemNums 4.0 //tabbar的數(shù)量

@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.6) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    badgeView.frame = CGRectMake(x, y, 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

調(diào)用:
//顯示
[self.tabBarController.tabBar showBadgeOnItemIndex:2];

//隱藏
[self.tabBarController.tabBar hideBadgeOnItemIndex:2]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容