iOS TabBarItem設(shè)置紅點(diǎn)(未讀消息)
實(shí)現(xiàn)原理:
其實(shí)是自定義一個(gè)view,將view添加到UITabBar上面,也可以是一個(gè)按鈕,設(shè)置背景圖片,和label。
廢話少說直接上代碼
搞一個(gè)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) //顯示紅點(diǎn) - (void)showBadgeOnItmIndex:(int)index{ [self removeBadgeOnItemIndex:index]; //新建小紅點(diǎn) 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]; } //隱藏紅點(diǎn) -(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
最后在子控制器調(diào)用就可以啦
[self.tabBarController.tabBar showBadgeOnItmIndex:4];
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
基于ios逆向過程中l(wèi)ldb調(diào)試技巧(推薦)
下面小編就為大家?guī)硪黄趇os逆向過程中l(wèi)ldb調(diào)試技巧(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07iOS開發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
這篇文章主要介紹了iOS開發(fā)中UILabel設(shè)置字體的相關(guān)技巧小結(jié),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)
這篇文章主要介紹了IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)的相關(guān)資料,希望通過此文大家能夠掌握輪播圖的實(shí)現(xiàn),需要的朋友可以參考下2017-09-09iOS App設(shè)計(jì)模式開發(fā)之適配器模式使用的實(shí)戰(zhàn)演練
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)之適配器模式的使用實(shí)例,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03詳解Obejective-C中將JSON數(shù)據(jù)轉(zhuǎn)為模型的方法
這篇文章主要介紹了Obejective-C中JSON數(shù)據(jù)轉(zhuǎn)為模型的方法,同時(shí)介紹了使用jastor庫的方法,需要的朋友可以參考下2016-03-03