2014-02-06 69 views
0

我想根據tabbar的選定索引更改tintColor屬性。我現在使用的代碼不起作用,所有tabbar視圖都有此代碼:如何根據TabBar選擇的索引設置色調顏色?

- (void)viewDidAppear:(BOOL)animated{ 

    switch (self.tabBarController.selectedIndex) { 
     case 0: 
      self.tabBarController.tintColor = [UIColor colorWithRed:147/255 green:22/255 blue:0/255 alpha:1.0]; 
      break; 

     case 1: 
      self.tabBarController.tintColor = [UIColor whiteColor]; 
      break; 

     case 2: 
      self.tabBarController.tintColor = [UIColor greenColor]; 
      break; 

     default: 
      break; 
    } 
} 

回答

2

您不需要那麼做;相反,把這個在您的包含控制器的viewWillAppear:方法:

控制器1:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    self.tabBarController.tabBar.tintColor = [UIColor blueColor]; 
} 

控制器2:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    self.tabBarController.tabBar.tintColor = [UIColor yellowColor]; 
} 

這假定的iOS5 +。希望這可以幫助。

0

而不是

self.tabBarController.tintColor 

用途:

self.tabBarController.tabBar.tintColor 
+1

這就是我的回答? :-)適當的給我一些信用。 – Unheilig

+0

@Unheilig保持冷靜,我不會... –