简体   繁体   English

选择后如何显示不同的标签栏图标颜色

[英]How have different tab bar icon color when selected

I want know if there is a way to have a different color for different icon selected in a UITabBarController , i know that with this: 我想知道是否有一种方法可以为UITabBarController选择的不同图标提供不同的颜色,我知道:

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

i can change the selection color for all icon, but how i can do it for different tab? 我可以更改所有图标的选择颜色,但是如何为其他选项卡呢?

One way of doing this, would be by simply changing the tint color of the tabbar in the different viewcontrollers. 一种方法是简单地更改不同视图控制器中标签栏的颜色。

Let's say for example you have 3 different tabs: tab1 , tab2 and tab3 , all of which are displaying different viewcontrollers. 假设您有3个不同的标签: tab1tab2tab3 ,它们都显示不同的viewcontrollers。 Now, let's say you want to have a blue tint color in tab1 but a red tint color in tab2 and tab3 . 现在,假设您要在tab1使用蓝色,但在tab2tab3使用红色。 Then, you can simply add the following line to the viewWillAppear: method of the different ViewControllers . 然后,您可以简单地viewWillAppear:下行添加到不同ViewControllersviewWillAppear:方法。

For the first viewController, which is shown in tab1 , you'd have: 对于第一个viewController(显示在tab1 ,您将具有:

//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]];
}

And for the other two, you simply put another color: 对于其他两个,您只需放置另一种颜色:

//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]];
}

It's that simple. 就这么简单。 There are certainly other ways of doing this, but this one is pretty clean and straightforward. 当然,还有其他方法可以做到这一点,但是这一方法非常干净直接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM