實現原理:
其實是自定義一個view,將view添加到UITabBar上面,也可以是一個按鈕,設置背景圖片,和label。
廢話少說直接上代碼
搞一個UITabBar的分類
#import <UIKit/UIKit.h>@interface UITabBar (Extension)- (void)showBadgeOnItmIndex:(int)index;- (void)hideBadgeOnItemIndex:(int)index;@end
#import "UITabBar+Extension.h"#define TabbarItemNums 5.0@implementation UITabBar (badge)//顯示紅點- (void)showBadgeOnItmIndex:(int)index{ [self removeBadgeOnItemIndex:index]; //新建小紅點 UIView *bview = [[UIView alloc]init]; bview.tag = 888+index; bview.layer.cornerRadius = 5; bview.clipsToBounds = YES; bview.backgroundColor = [UIColor redColor]; CGRect tabFram = self.frame; float percentX = (index+0.6)/TabbarItemNums; CGFloat x = ceilf(percentX*tabFram.size.width); CGFloat y = ceilf(0.1*tabFram.size.height); bview.frame = CGRectMake(x, y, 10, 10); [self addSubview:bview]; [self bringSubviewToFront:bview];}//隱藏紅點-(void)hideBadgeOnItemIndex:(int)index{ [self removeBadgeOnItemIndex:index];}//移除控件- (void)removeBadgeOnItemIndex:(int)index{ for (UIView*subView in self.subviews) { if (subView.tag == 888+index) { [subView removeFromSuperview]; } }}@end
最后在子控制器調用就可以啦
[self.tabBarController.tabBar showBadgeOnItmIndex:4];
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答