2014-02-06 71 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保持冷静,我不会... –