0

I am trying to make the font size larger for my tab bar items. I have tried doing this in 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];

I also tried defining the dictionary like so:

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

It's in a custom function that is called via a delegate protocol once the user logs in. My NSLogs print out, but I also get this in the console:

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

The program runs as desired other than that the title fonts do not change at all with this command despite the log outputs.

So then I put this inside viewDidLoad for my custom view controllers:

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

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

But this also did not affect the font size of the tab bar titles.

Where should I be executing these commands?

Thank you.


addendum The marked answer does what I needed, but I noticed that the method is only working if I use the class-wide setter rather than an instance setter. So to change the tab bar item title text globally, this works like a charm:

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

but trying to set the attributes of a single class instance still isn't working (these were both called in the same function, but only one takes effect).

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

Anyone know why this is? I've gone over the Apple Dev docs multiple times without seeing why the class instance call is not doing anything.

1 Answers1

0

I have used this code in didFinishLaunchingWithOptions and it worked for me.

NSDictionary *attribute = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];
gabbler
  • 13,626
  • 4
  • 32
  • 44