1. 程式人生 > >iOS開發專案篇—26修改UITabBar的系統設定

iOS開發專案篇—26修改UITabBar的系統設定

複製程式碼
  1 //
  2 //  YYTabBarViewController.m
  3 //
  4 
  5 #import "YYTabBarViewController.h"
  6 #import "YYHomeTableViewController.h"
  7 #import "YYDiscoverViewController.h"
  8 #import "YYMessageViewController.h"
  9 #import "YYProfileViewController.h"
 10 #import "UIImage+Extension.h"
 11 #import "YYNavigationViewController.h
" 12 13 @interface YYTabBarViewController ()<UITabBarControllerDelegate> 14 15 @end 16 17 @implementation YYTabBarViewController 18 19 20 - (void)viewDidLoad 21 { 22 [super viewDidLoad]; 23 //新增四個子控制器 24 YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init];
25 [self addOneChildVc:home title:@"首頁" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"]; 26 27 28 YYMessageViewController *message=[[YYMessageViewController alloc]init]; 29 [self addOneChildVc:message title:@"訊息" imageName:@"tabbar_message_center" selectedImageName:@"
tabbar_message_center_selected"]; 30 31 YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init]; 32 [self addOneChildVc:discover title:@"發現" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"]; 33 34 YYProfileViewController *profile=[[YYProfileViewController alloc]init]; 35 [self addOneChildVc:profile title:@"" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"]; 36 37 // self.tabBar.backgroundImage=[UIImage imageWithName:@"tabbar_background"]; 38 self.delegate=self; 39 40 } 41 42 -(void)viewDidAppear:(BOOL)animated 43 { 44 [super viewDidAppear:animated]; 45 46 [self removeBackgroundInTabBarButton]; 47 } 48 49 -(void)removeBackgroundInTabBarButton 50 { 51 for (UIView *child in self.tabBar.subviews) { 52 //如果不是UITabBarButton,就過掉 53 if (![child isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue; 54 55 BOOL selected=NO; 56 57 for (UIView *childchild in child.subviews) { 58 if ([childchild isKindOfClass:NSClassFromString(@"UITabBarSelectionIndicatorView")]) {//說明是個選中的背景 59 [childchild removeFromSuperview]; 60 selected = YES; 61 } 62 63 if ([childchild isKindOfClass:[UILabel class]]) {//說明是個UIlable 64 UILabel *label = (UILabel *)childchild; 65 // label.font=[UIFont systemFontOfSize:15]; 66 if (selected) {//說明這個button選中,設定文字的顏色為橙色 67 label.textColor=[UIColor orangeColor]; 68 }else // 說明這個button沒有選中,設定label顏色為黑色 69 { 70 label.textColor=[UIColor blackColor]; 71 } 72 } 73 } 74 } 75 } 76 77 78 -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 79 { 80 [self removeBackgroundInTabBarButton]; 81 } 82 83 /** 84 * 新增一個子控制器 85 * 86 * @param childVC 子控制物件 87 * @param title 標題 88 * @param imageName 圖示 89 * @param selectedImageName 選中時的圖示 90 */ 91 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName 92 { 93 //隨機設定子控制器的背景顏色 94 // childVc.view.backgroundColor=YYRandomColor; 95 96 //設定標題 97 childVc.title=title; //相當於設定了後兩者的標題 98 // childVc.navigationItem.title=title;//設定導航欄的標題 99 // childVc.tabBarItem.title=title;//設定tabbar上面的標題 100 101 //設定圖示 102 childVc.tabBarItem.image=[UIImage imageWithName:imageName]; 103 //設定選中時的圖示 104 UIImage *selectedImage=[UIImage imageWithName:selectedImageName]; 105 106 107 if (iOS7) { 108 // 宣告這張圖片用原圖(別渲染) 109 selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 110 } 111 childVc.tabBarItem.selectedImage = selectedImage; 112 113 // 新增為tabbar控制器的子控制器 114 YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc]; 115 116 [self addChildViewController:nav]; 117 118 } 119 120 121 // 在iOS7中, 會對selectedImage的圖片進行再次渲染為藍色 122 // 要想顯示原圖, 就必須得告訴它: 不要渲染 123 124 // Xcode的外掛安裝路徑: /Users/使用者名稱/Library/Application Support/Developer/Shared/Xcode/Plug-ins 125 @end
複製程式碼