我有一个TabBar。我试图设置它的样式,以便TabBarItems上的标题具有正常状态和选定状态的不同字体。对于正常状态,我想要Helvetica Neue Light,对于选定状态,我想要Helvetica Neue Medium。无论我做什么,我似乎都无法让这两种状态的字体不同。颜色变化很好。这是我现在拥有的:
// 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];
请帮忙。
答案 0 :(得分:11)
好消息和坏消息。
坏消息,这比使用外观代理要困难一些。
好消息这不是那么难!
标题
#import <UIKit/UIKit.h>
@interface MYTabbyViewController : UITabBarController
@end
实施
#import "MYTabbyViewController.h"
@implementation MYTabbyViewController
-(void)setSelectedViewController:(UIViewController *)selectedViewController
{
[super setSelectedViewController:selectedViewController];
for (UIViewController *viewController in self.viewControllers) {
if (viewController == selectedViewController) {
[viewController.tabBarItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Medium" size:16],
NSForegroundColorAttributeName: [UIColor blueColor]
}
forState:UIControlStateNormal];
} else {
[viewController.tabBarItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:16],
NSForegroundColorAttributeName: [UIColor grayColor]
}
forState:UIControlStateNormal];
}
}
}
您需要的最后一部分是使用此子类而不是开箱即用的UITabBarController。如果您正在使用Storyboard,只需选择TabBarController,转到Identity Inspector(右侧面板中的第三个子选项卡)并将UITabBarController更改为MYTabBarController或您拥有的内容。
但是啊!底线,只需更新ViewController tabBarItem!
答案 1 :(得分:6)
@Swiny在Swift 3中回答:
override var selectedViewController: UIViewController? {
didSet {
guard let viewControllers = viewControllers else {
return
}
for viewController in viewControllers {
if viewController == selectedViewController {
let selected: [String: AnyObject] =
[NSFontAttributeName: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy),
NSForegroundColorAttributeName: UIColor.red]
viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)
} else {
let normal: [String: AnyObject] =
[NSFontAttributeName: UIFont.systemFont(ofSize: 11),
NSForegroundColorAttributeName: UIColor.blue]
viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)
}
}
}
}
答案 2 :(得分:2)
@Acey's Answer几乎是完美的,但是只有在您第一次选择tabBarItem之后才更改字体,因此,创建tabBar时,将在那时为所选的tabBarItem创建没有不同字体的字体。为此,我还要在didSet
变量中添加一个selectedIndex
。
这是完整的代码(Swift 4.2)
final class TabBarController: UITabBarController {
override var selectedIndex: Int {
didSet {
guard let selectedViewController = viewControllers?[selectedIndex] else {
return
}
selectedViewController.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy)], for: .normal)
}
}
override var selectedViewController: UIViewController? {
didSet {
guard let viewControllers = viewControllers else {
return
}
for viewController in viewControllers {
if viewController == selectedViewController {
let selected: [NSAttributedString.Key: AnyObject] =
[.font: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy)]
viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)
} else {
let normal: [NSAttributedString.Key: AnyObject] =
[.font: UIFont.systemFont(ofSize: 11)]
viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)
}
}
}
}
}
答案 3 :(得分:1)
setSelected和setHighlighted对UIBarButtonItems不起作用。
使用UIBarButtonItem的
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
或者,使用UIButton。
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[myButton setTitle:@"Info" forState:UIControlStateNormal];
[myButton setFont:[UIFont fontWithName:<font name> size:<font size>]];
[myButton setTitleColor:<color>;
myButton = <frame>;
[myButton addTarget:self.webView action:@selector(<action>:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myButton];
[[UIBarButtonItem appearance] setTintColor:<color>;
[self.toolbar setItems:[NSArray arrayWithObjects: spaceItem, barButtonItem, nil]];