How to Set Image in Tabbar Not Tint Color in iOS

How to set Image in Tabbar not Tint color in ios?

You need to set your image attributes property Render As to Original Image. To change this select your image from assets and in the Attributes Inspector set Render As property to Original Image like this.

Sample Image

Changing tab bar item image and text color iOS

From UITabBarItem class docs:

By default, the actual unselected and selected images are
automatically created from the alpha values in the source images. To
prevent system coloring, provide images with
UIImageRenderingModeAlwaysOriginal.

The clue is not whether you use UIImageRenderingModeAlwaysOriginal, the important thing is when to use it.

To prevent the grey color for unselected items, you will just need to prevent the system colouring for the unselected image. Here is how to do this:

var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem

As you can see, I asked iOS to apply the original color (white, yellow, red, whatever) of the image ONLY for the UNSELECTED state, and leave the image as it is for the SELECTED state.

Also, you may need to add a tint color for the tab bar in order to apply a different color for the SELECTED state (instead of the default iOS blue color). As per your screenshot above, you are applying white color for the selected state:

self.tabBar.tintColor = UIColor.whiteColor()

EDIT:

Sample Image

Swift: Change the image tint color of tab bar?

In your 'application:didFinishLaunchingWithOptions'

(window?.rootViewController as! UITabBarController).tabBar.tintColor = UIColor.red

or use appearance delegate.

UITabBar.appearance().tintColor = UIColor.red

UITabBar not changing tint color Xcode 9.3

Basically, when you want to change the tint color of UITabBar programmatically, UITabBar class gives you several tint color properties:

  • tintColor: TabBarItem's color.
  • barTintColor : TabBar's background bar's color.
  • unselectedItemTintColor : color of unselected items.

so if you change the tintColor, barItems' color would be changed.



...but, Why it doesn't works on IB?

When you set a specific color to UITabBar's item in IB, there's an option named Image Tint.

imageTint

Changing a Tint option on "View" section won't affect anything to TabBar's items but only Image Tint option can change tabBar's item color.

storyboard's global tint color option changes Tint option of "View" section, but doesn't affect default value of Image Tint option, so It doesn't affect the tab bar's tint color.



So.. Why Image Tint option doesn't affected?

I can't explain why doesn't it affected. Maybe Apple had an issue with this, or kind of bug.


there are some workarounds for setting an image color :

  • Explicitly Set an Image Tint option to UITabBarController's TabBar object.

You may should set every TabBarController's Image Tint option, because it doesn't affects global setting.

  • Programmatically change global UITabBar's tintColor.

At AppDelegate.swift's didFinishLaunchingWithOptions, paste following code

UITabBar.appearance().tintColor = <#Color what you want#>

How do you change the color of unselected items in a Tab Bar?

Make the icon images Black, if they are white Im not sure it will work. As in the actual image themselves should be black in the assets folder not white.

If you add a tabbar from the story board you can put these line of code in that method in the appDelegate method shown in your post

UITabBar.appearance().barTintColor = UIColor.black
UITabBar.appearance().tintColor = UIColor.red
UITabBar.appearance().unselectedItemTintColor = .white


Related Topics



Leave a reply



Submit