选择时如何具有不同的选项卡栏图标颜色

How have different tab bar icon color when selected

我想知道是否有办法为 UITabBarController 中选择的不同图标设置不同的颜色,我知道是这样的:

[[UITabBar appearance] setTintColor:[UIColor whiteColor]];

我可以更改所有图标的选择颜色,但如何为不同的选项卡更改选择颜色?

一种方法是简单地改变不同视图控制器中标签栏的色调。

假设您有 3 个不同的选项卡:tab1tab2tab3,它们都显示不同的视图控制器。现在,假设您想在 tab1 中使用蓝色色调,而在 tab2tab3 中使用红色色调。 然后,您可以简单地将以下行添加到不同 ViewControllers.

viewWillAppear: 方法中

对于 tab1 中显示的第一个 viewController,您将拥有:

//In your viewcontroller which is shown in tab1
- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   // change tint color to blue
   [self.tabBarController.tabBar setTintColor:[UIColor blueColor]];
}

对于另外两个,您只需添加另一种颜色:

//In your viewcontrollers which are shown in tab2 and tab3
- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   // change tint color to red
   [self.tabBarController.tabBar setTintColor:[UIColor redColor]];
}

就这么简单。当然还有其他方法可以做到这一点,但这个非常简单明了。