简体   繁体   中英

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:

[[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. Now, let's say you want to have a blue tint color in tab1 but a red tint color in tab2 and tab3 . Then, you can simply add the following line to the viewWillAppear: method of the different ViewControllers .

For the first viewController, which is shown in tab1 , you'd have:

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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