4

I'm trying to change the font size of just one of my view controllers because it lends itself to more text, thus requiring smaller font size than my other views. I am setting the navigation bar attributes in the appdelegate during didFinishLaunchingWithOptions, and then setting the font for my view controller in question during the viewDidLoad. However, when I go back a screen, or forward, the changes I made to the navigation bar are retained, i.e. the smaller font. Is there a way around this? For example setting the font back to normal upon exiting the view or something?

Cliffsnotes: Trying to set font in just one view controller, but once I set it, it applies to all view controllers.

Code is as follows:

In AppDelegate:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor clearColor],
  UITextAttributeTextColor,
  [UIColor whiteColor],
  UITextAttributeTextShadowColor,
  [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
  UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"QuicksandBold-Regular" size:24.0],
  UITextAttributeFont,
  nil]];

In the view controller viewDidLoad:

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor clearColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor whiteColor],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"QuicksandBold-Regular" size:18.0],
                                                                     UITextAttributeFont,
                                                                     nil]];
Hudson Buddy
  • 726
  • 2
  • 9
  • 20
  • I tried to edit the title color with your solution: `NSDictionary *titleTextAttributes = [[UINavigationBar appearance] titleTextAttributes]; [titleTextAttributes setValue:[UIColor colorWithRed:0.48627454040000001 green:0.050980396570000003 blue:0.13725490870000001 alpha:1] forKey:UITextAttributeTextColor]; [[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes];` But doesn't work :S! – IgniteCoders Mar 10 '14 at 12:39

2 Answers2

3

The navbar is shared by all view controllers in the navigation controller. So once you change the navbar, all view controllers in the same nav stack will be affected. One option would be to set a custom titleView for this one view controller.

self.navigationItem.titleView = ... // some UILabel with the desired formatting

This way, only the one view controller shows the custom title.

Another option would be to change the navbar's appearance in the view controller's viewWillAppear method and then reset it in the view controller's viewWillDisappear method. But this approach may not be as smooth as just using a custom titleView.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

So, now yours attributed keys is deprecated. Use this keys:

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
 [UIColor clearColor], NSForegroundColorAttributeName,
 [UIColor whiteColor], NSShadowAttributeName,
 [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], NSShadowAttributeName,
 [UIFont fontWithName:@"QuicksandBold-Regular" size:24.0], NSFontAttributeName,
 nil];