RDVTabBarController這個庫寫得相當不錯,所以今天就簡單介紹下它的基本使用,看裡面可以清楚的知道代碼規範的重要性,這個庫的使用方法和官方的相識 下載地址:https://github.com/robbdimitrov/RDVTabBarController 首先寫過控制器繼承 RDV ...
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 @end 10 11 @implementation RDVViewController 12 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 }