简体   繁体   中英

how do you change the tint color of an unselected image in a tab bar in iOS?

如何在iOS的标签栏中更改未选中图像的色调颜色?

Unfortunately, there's no way to do that. There was a feature allowing you to do that, but it was removed in iOS 7 and has never been restored.

Your only option, if you want that kind of control, is not to use the tint color at all, but to set the image and the selected image, as normal images (rendered using AlwaysOriginal , not template images). That way, there's no tinting, and the selected image is used when the item is selected, and the regular image is used when it is not, and the colors are completely up to you.

You can can set the tab bar image tint color for the selected image programmatically, then if you want to tint the unselected image you should make the image yourself with the desired color. I like to do the same color as my selected image but with 50% opacity. The code looks like this:

UIImage *item2Image = [[UIImage imageNamed:@"simulatorTabBarIconUnselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *item2ImageSelected = [[UIImage imageNamed:@"simulatorTabBarIconSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Simulator" image:item2Image selectedImage:item2ImageSelected];

The always rendering mode UIImageRenderingModeAlwaysOriginal means the unselected image will appear as it is in your image assets folder, whereas the UIImageRenderingModeAlwaysTemplate will cause the image to appear as the tint color you've specified. You can see how this looks here:

http://www.apppicker.com/apps/1059513718/jetfuel-by-beaconsinspace

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