I have a TabBar. I am trying to style it so that the titles on the TabBarItems have different fonts for the normal state and the selected state. For normal state I want Helvetica Neue Light and for the selected state I want Helvetica Neue Medium. No matter what I do, I can't seem to get the fonts to be different for these two states. The color changing works fine. Here is what I currently have:
// Set the tab bar title appearance for normal state.
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light"
size:16],
NSForegroundColorAttributeName: [CMK8Colors grayColor]
}
forState:UIControlStateNormal];
// Set the tab bar title appearance for selected state.
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Medium"
size:16],
NSForegroundColorAttributeName: [CMK8Colors blueColor]
}
forState:UIControlStateSelected];
Please help.
@Acey´s answer in Swift 3:
Good news and bad news.
Bad news, It's a little harder than just using the appearance proxy.
Good news It's not that much harder!
Header
Implementation
The last part you will need is to use this subclass instead of the out-of-the-box UITabBarController. If you are using Storyboards, simply select the TabBarController, go to the Identity Inspector (third subtab in the right panel) and change UITabBarController to MYTabBarController or what have you.
But yeah! Bottom line, just update the ViewController tabBarItem!
@Acey's Answer is almost perfect, but only change the font after your first selection of a tabBarItem, so when the tabBar is created, it will be created with no different font for the selected tabBarItem in that moment. To achieve that i also add a
didSet
inselectedIndex
variable.Here's the Complete code (Swift 4.2)
}
setSelected and setHighlighted does not work for UIBarButtonItems.
Use UIBarButtonItem's
Here is the documentation - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html
Alternatively, use UIButton.