Customize the text color of the selected tab bar item

I created a tab bar app. I have four tabs; on the selected tab, I need to set a red color for the tab header. How can i do this?

Thanks in advance.

+8
iphone uitabbar
source share
6 answers

If I understand your question correctly, you want to adjust the text color on UITabBarItem s. Unfortunately, this is really not so flexible. If you intend to do this (which, if you did not carefully consider the design with the help of a professional, I recommend against it!), You will need to do some really scary things to make it work.

I suggest repeating through subviews UITabBar (as many levels as necessary) and looking for UILabel objects. If you find them, you can change their color. If you do not, this means that it is implemented in different ways (probably in the -drawRect: method -drawRect: somewhere); if that happens, you really have to refuse.

Good luck with what you decide to do.

0
source share

Using the UIAppearance protocol (iOS5 +), this is now possible, and actually quite easy.

 [UITabBarItem.appearance setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal]; [UITabBarItem.appearance setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected]; 

Sorry for the awful colors!

+22
source share

Just to figure it out a bit ...

If you want to change the appearance for all elements of the tab bar, use:

Objective-C:

 [[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected]; 

Swift:

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected) 

However , if you just want to set the appearance of one element, do this:

Objective-C:

 [self.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected]; 

Swift:

 tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected) 

Note : tabBarItem is a property on a UIViewController . This means that although every property of the UIViewController has this property, it may not be the tabBarItem you are looking for. This often happens when your view controller is enclosed in a UINavigationController . In this case, refer to tabBarItem on the navigation controller, and not to the root controller (or another).

+2
source share

Here is what finally helped me:

1) Selected text color

 [[UIView appearance] setTintColor:someColor]; 

2) Unselected text (also changes the color of the image)

 [[UITabBar appearance] setTintColor:anotherColor]; 
+1
source share

This is the quick version: -

  for item in self.mainTabBar.items! { let unselectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] let selectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] item.setTitleTextAttributes(unselectedItem as? [String : AnyObject], forState: .Normal) item.setTitleTextAttributes(selectedItem as? [String : AnyObject], forState: .Selected) } 
+1
source share

This is possible with -drawRect: but by doing this, you greatly increase the chances of your application rejecting in the App Store

-one
source share

Source: https://habr.com/ru/post/649825/


All Articles