更改选定的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.如果您正在使用Storyboard,只需选择TabBarController,转到Identity Inspector(右侧面板中的第三个子选项卡)并将UITabBarController更改为MYTabBarController或您拥有的内容.

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

以上是来客网为你收集整理的更改选定的TabBarItem字体iOS全部内容,希望文章能够帮你解决更改选定的TabBarItem字体iOS所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。