更改选定的TabBarItem字体iOS

我有一个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]; 

请帮忙。

好消息和坏消息。

坏消息 ,这比使用外观代理有点困难。

好消息没有那么难!

 #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。 如果您正在使用故事板,只需selectTabBarController,转到Identity Inspector(右侧面板中的第三个子选项卡),然后将UITabBarController更改为MYTabBarController或您有什么。

但是,是的! 底线,只需更新ViewController tabBarItem!

@ Acey在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) } } } } 

setSelected和setHighlighted不适用于UIBarButtonItems。

使用UIBarButtonItem的

 - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics 

这里是文档 – https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html

或者,使用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]];