ios - 更改选定的 TabBarItem 字体 iOS

标签 ios uitabbar uitabbaritem

我有一个 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 - 更改选定的 TabBarItem 字体 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234671/

相关文章:

ios - 横向模式下的 TabBar 图像大小

ios - UINavigationBar 在屏幕截图中显示为灰色

swift - 在 swift 自定义 UITabBar 中选择项目

ios - 如何在点击 tabBar 项目 Swift 上刷新 tableView?

ios - 自定义标签栏 iOS

ios - 通过访问 AppDelegate 中的 Tab Bar Items 更改 Tab Bar Item Image inset

swift - 在 appDelegate 中设置 UITabBarItem 徽章值

ios - 为什么不将值user.username传递给导航器

ios - objective-c : is Posing available for ios?

objective-c - 如何在 iOS 中将任何类型的文件附加到电子邮件