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 }