The Lone Coder
The Lone Coder

Reputation: 3104

How to set Title for UITabBarItem without changing tint color

When I change the title of a UITabBarItem in a UITabBar programmatically from a UIViewController in another tab, the title changes correctly, but the image of the UITabBarItem changes its color to the tintColor or the tabbar.

Here is what is looks like after I set the title of the third tab within the UIViewController of the second tab like this

self.tabBarController?.tabBar.items![2].title = "9 Places"

After setting the title for the third tab

Any idea how I can prevent the third tab icon from changing the tint color?

Using Xcode 10.1 and iOS 12.1.1.

Thanks!

Upvotes: 2

Views: 149

Answers (2)

The Lone Coder
The Lone Coder

Reputation: 3104

The answer from @Gallo Torres Sevilla did not work for me, but it did point me in the right direction. So thanks for this.

Basically when setting up the UITabBarItem for the first time, I needed to supply the image including the renderingMode as suggested by Gallo as well as the selectedImage. Then subsequent changes to the title do not impact the tint.

Upvotes: 0

Galo Torres Sevilla
Galo Torres Sevilla

Reputation: 1575

I think you will be better off setting the title and the image together.

self.tabBarItem = UITabBarItem(title: "9 Places", image: yourImage.withRenderingMode(.alwaysOriginal), selectedImage: yourImage)

Upvotes: 1

Related Questions