更改选定的TabBarItem字体iOS

前端之家收集整理的这篇文章主要介绍了更改选定的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];

请帮忙.

解决方法

@H_403_9@ 好消息和坏消息.

坏消息,这比使用外观代理要困难一些.

好消息它并没有那么难!

#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!

原文链接:https://www.f2er.com/iOS/334996.html

猜你在找的iOS相关文章