How can I set the titletextattributes for a UINavigation bar for just one view controller?

Hudson Buddy picture Hudson Buddy · Oct 9, 2012 · Viewed 13.6k times · Source

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]];

Answer

rmaddy picture rmaddy · Oct 10, 2012

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.