更改选定的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!

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...