RDVTabBarController的基本使用 以及tabbar的防止雙點擊方法,rdvtabbarcontroller
RDVTabBarController這個庫寫得相當不錯,所以今天就簡單介紹下它的基本使用,看裡面可以清楚的知道代碼規範的重要性,這個庫的使用方法和官方的相識
:https://github.com/robbdimitrov/RDVTabBarController
首先寫過控制器繼承 RDVTabBarController,
AppDelegate裡面的不多說
例如
1 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];2 3 RDVViewController *rootViewController = [[RDVViewController alloc] init];4 self.window.rootViewController = rootViewController;5 [self.window makeKeyAndVisible];
然後在
RDVViewController.m檔案裡
為了防止tabbar的雙點擊事件,設定代理<RDVTabBarControllerDelegate>,添加標示,用在點擊事件
要設定tabbaritem的字型和圖片,需匯入檔案
#import "RDVTabBarItem.h"
1 #import "RDVViewController.h" 2 #import "ViewController.h" 3 #import "RDVTabBarItem.h" 4 5 @interface RDVViewController ()<RDVTabBarControllerDelegate> 6 { 7 NSInteger selectedTabBarIiemTag; 8 } 9 @end10 11 @implementation RDVViewController12 13 - (void)viewDidLoad {14 [super viewDidLoad];15 16 ViewController *homeView=[[ViewController alloc]init];17 UINavigationController *NAV1 = [[UINavigationController alloc] initWithRootViewController:homeView];18 19 ViewController *View=[[ViewController alloc]init];20 UINavigationController *NAV2 = [[UINavigationController alloc] initWithRootViewController:View];21 22 ViewController *home=[[ViewController alloc]init];23 UINavigationController *NAV3 = [[UINavigationController alloc] initWithRootViewController:home];24 25 self.viewControllers = @[NAV1,NAV2,NAV3];26 [self customizeTabBarForController:self];27 self.delegate = self;28 }29 30 - (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {31 32 NSArray *tabBarItemImages = @[@"首頁select", @"首頁select",@"首頁select"];33 34 NSInteger index = 0;35 for (RDVTabBarItem *tabberItem in [[tabBarController tabBar] items]) {36 37 tabberItem.title = [NSString stringWithFormat:@"%ld",index+1];38 39 NSDictionary *tabBarTitleUnselectedDic = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:15]};40 NSDictionary *tabBarTitleSelectedDic = @{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:15]};41 //修改tabberItem的title顏色42 tabberItem.selectedTitleAttributes = tabBarTitleSelectedDic;43 tabberItem.unselectedTitleAttributes = tabBarTitleUnselectedDic;44 tabberItem.tag = 100+index;45 UIImage *selectedimage = [UIImage imageNamed:@"首頁"];46 UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",47 [tabBarItemImages objectAtIndex:index]]];48 //設定tabberItem的選中和未選中圖片49 [tabberItem setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];50 51 index++;52 }53 }
上面就滿足基本使用方法了,為了防止雙點擊事件,已經設定了代理和tag值
這裡的item不是tabbaritem,而是 rdv_tabBarItem,如果使用了tabBarItem的tag值,那是不存在的,因為這裡設定的是rdv的tabBarItem
1 #pragma mark - 防止tabbar雙擊 2 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 3 4 if(selectedTabBarIiemTag == viewController.rdv_tabBarItem.tag){ 5 6 return NO; 7 8 }else { 9 10 selectedTabBarIiemTag = viewController.rdv_tabBarItem.tag;11 return YES;12 13 }14 }