首页 文章

where / when /如何设置tabBarItem字体属性属性?

提问于
浏览
0

我试图使我的标签栏项目的字体更大 . 我试过在AppDelegate中这样做:

MenuTableViewController *mvc = [[MenuTableViewController alloc] init];
mvc.delegate = self;
mvc.restorationIdentifier = NSStringFromClass([mvc class]);
mvc.restorationClass = [self class];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:mvc];
SuperTabBarController *nav = [[SuperTabBarController alloc] init];
JudgeViewController *jvc = [[JudgeViewController alloc] init];
BuzzViewController *bvc = [[BuzzViewController alloc] init];
nav.viewControllers = @[jvc, bvc, nav1];

UIFont *tabBarFont = [UIFont systemFontOfSize:40];
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     tabBarFont, NSFontAttributeName, nil];
for(UIViewController *tab in  nav.viewControllers) {
    [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
    NSLog(@"yo normal");
}

for(UIViewController *tab in  nav.viewControllers) {
    [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateSelected];
    NSLog(@"yo");      
}


[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{self.window.rootViewController = nav;} completion:nil];

我也试过像这样定义字典:

NSDictionary *titleTextAttributes2 = @{NSFontAttributeName:
                                               [UIFont fontWithName:@"Helvetica" size:20]};

它位于一个自定义函数中,一旦用户登录就通过委托协议调用 . 我的NSLogs打印出来,但我也在控制台中得到了这个:

button text attributes only respected for UIControlStateNormal, UIControlStateSelected and UIControlStateDisabled. state = 1 is interpreted as UIControlStateSelected.

该程序按照需要运行,除了日志输出之外,使用此命令 Headers 字体完全不会更改 .

那么我把它放在我的自定义视图控制器的viewDidLoad中:

UIFont *tabBarFont = [UIFont systemFontOfSize:40];
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     tabBarFont, NSFontAttributeName, nil];

    [self.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
    [self.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateSelected];

但这也不会影响标签栏 Headers 的字体大小 .

我应该在哪里执行这些命令?

谢谢 .


附录标记的答案可以满足我的需要,但是我注意到只有使用类范围的setter而不是实例setter才能使用该方法 . 因此,要全局更改标签栏项目 Headers 文本,这就像一个魅力:

NSDictionary *attribute =     @{NSFontAttributeName:
                                    [UIFont fontWithName:@"Helvetica" size:40]};
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];

但是尝试设置单个类实例的属性仍然不起作用(这些都在同一个函数中调用,但只有一个生效) .

NSDictionary * attribute2 = @ {NSFontAttributeName:[UIFont fontWithName:@“Helvetica”size:10]}; [nav1.tabBarItem setTitleTextAttributes:attribute2 forState:UIControlStateNormal];

有谁知道这是为什么?我已多次浏览Apple Dev docs而没有看到为什么类实例调用没有做任何事情 .

1 回答

  • 0

    我在 didFinishLaunchingWithOptions 中使用过这段代码,它对我有用 .

    NSDictionary *attribute = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    [[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];
    

相关问题