Settitletextattributes Doesn't Work for Uitabbaritem When It Is Unselected in Swift

setTitleTextAttributes doesn't work for UITabBarItem when it is unselected in swift

Here is the code that works in this case to put still in the UITabBarController:

override func viewDidLoad() {
super.viewDidLoad()

//custom tab bar
self.tabBar.barTintColor = UIColor(red: 0.0/255.0, green: 102.0/255.0, blue: 153.0/255.0, alpha: 1)
self.tabBar.tintColor = UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)

for item in self.tabBar.items as [UITabBarItem]
{
item.image = item.selectedImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Disabled)
item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 171.0/255.0, green: 203.0/255.0, blue: 61.0/255.0, alpha: 1)], forState:UIControlState.Selected)
}
}

Unselected UITabBar color?

SO says i cannot delete the accepted answer (i tried), but obviously, there are a lot of upvotes for comments that this doesn't work for iOS 7.

See the other answer below with many more upvotes, or the link in @Liam's comment to this answer.


for iOS 6 only

It should be as simple as this:

[[UITabBar appearance] setTintColor:[UIColor grayColor]]; // for unselected items that are gray
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]]; // for selected items that are green

Customizing text color AND font of UITabBarItem causing weird result in swift

Try the following

let colorNormal : UIColor = UIColor.blackColor()
let colorSelected : UIColor = UIColor.whiteColor()
let titleFontAll : UIFont = UIFont(name: "American Typewriter", size: 13.0)!

let attributesNormal = [
NSForegroundColorAttributeName : colorNormal,
NSFontAttributeName : titleFontAll
]

let attributesSelected = [
NSForegroundColorAttributeName : colorSelected,
NSFontAttributeName : titleFontAll
]

UITabBarItem.appearance().setTitleTextAttributes(attributesNormal, forState: .Normal)
UITabBarItem.appearance().setTitleTextAttributes(attributesSelected, forState: .Selected)

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

Change tintColor of unselected UITabBarController item title and background image

Figured it out!

Use this to change the color of the text:

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateSelected];

And make sure that image rendering mode is set to ORIGINAL for the images

UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

How to customize tintColor and resize UITabBarItem

EDIT:
You are missing breaks in your switch statement:

switch item.tag{

Also, you are doing a switch on the tag and I don't see anywhere you have tagged them accordingly in your code. You should get the index of the item instead.

I am not a Swift coder, this is how you do it in Objective-C to give you a hint:

NSInteger indexOfTab = [[self.tabBar items] indexOfObject:item];

Then you do your switch statement of indexOfTab.

Here is the Swift version.:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the selected index is : \(tabBar.items.index(of: item))")
}

If you want to individually change the "tintColor" , you should set a custom selectedImage instead.

Beware:

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

As far as the documentation goes, there are no "tintColor" property for a UITabBarItem.

However, the UITabBar itself has a tintColor property. But this is not setting anything individually.

Tint Color

You can specify a custom tint color for the bar background using the
Tint (barTintColor) field. The default background tint color is white.

Use the Image Tint (selectedImageTintColor) field to specify the bar
item’s tint color when that tab is selected. By default, that color is
blue.

Regarding your resize methods, you should resize your original image instead, or check this question if it does fit your needs. However, the UITabBar and UITabBarItem customizations are limited to what you can read in the documentations.

If you want to further customize things individually, I suggest you search for or create a custom solution instead.

iPadOS 15 UITabBar title cut off

For some reason, it seems that setting the titleTextAttributes is what causes the problem to happen with inlineLayoutAppearance, and including the default paragraph style of NSParagraphStyle.default fixes it.

For your code, the following changes should fix it (as of iOS 15.0).

let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray, .paragraphStyle: NSParagraphStyle.default]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red, .paragraphStyle: NSParagraphStyle.default]

(iOS 9 and below) How do you specify tab bar item text color during user interactions (both selected and normal state)

I got it. It turns out specifying attributes individually will work rather than globally.

tabBar.items?[index].setTitleTextAttributes([NSForegroundColorAttributeName: normalColor], for: .normal)

tabBar.items?[index].setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor], for: .selected)



Related Topics



Leave a reply



Submit