Search code examples
iosuinavigationcontrolleruinavigationbartitleview

Customising same UINavigationBar within app multiple times


I am customising UINavigationBar with different color and custom font by using titleTextAttributes. However, when I moved to another view, I would like to use different color from previous with same custom font.

I have used,

[[UINavigationBar appearance] setTitleTextAttributes:mySettings]

in AppDelegate.m. When call same method with newSettings in viewDidLoad of another viewController, it doesn't get reflected.

I am able to change bar color or bar tint color in viewDidLoad of another viewController. However, my title foreground color is not changing. Am I missing anything?

Last solution which I have to have custom titleView. But wanted to avoid it. Any inputs?


Solution

  • [[UINavigationBar appearance] setTitleTextAttributes:mySettings] is a global configuration, so that's the reason why changing it in the other view controllers doesn't work. I believe you won't getting around customizing the titleView property of navigation item,... :/

    You can do this by creating a UILabel that has the settings that you want and then assign it to the property:

     NSAttributedString *title = [[NSAttributedString alloc] string:"the title" attributes:mySettings];
     UILabel *newTitleView = [[UILabel alloc] init];
     newTitleView.attributedText = title;
     self.navigationItem.titleView = newTitleView;