0

I am trying to set the color of my navigation bar. I have added my font in the plist file and added it as a resource, and I can select it as font in the storyboard when using a UILabel. So the font seems to be there.

I have tried this in the app delegate:

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:myLightBrownColor, NSForegroundColorAttributeName,[UIFont fontWithName:@"Pacifico" size:17.0f], NSFontAttributeName,
                                  myShadow, NSShadowAttributeName, nil];
[navigationBarAppearance setTitleTextAttributes:textTitleOptions];

And this in the view controller directly:

NSShadow *myShadow = [MRStyleController getMyShadow];
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[MRStyleController getLightBrownColor], NSForegroundColorAttributeName,[UIFont fontWithName:@"Pacifico" size:17.0f], NSFontAttributeName,
                                  myShadow, NSShadowAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = textTitleOptions;

However, none of this did change the apps navigation bar title font and color.

Any suggestions?

EDIT: what correctly works is:

UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
[navigationBarAppearance setBarTintColor:myBrownColor];

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:myLightBrownColor,      NSForegroundColorAttributeName,
    myShadow, NSShadowAttributeName,
    [UIFont fontWithName:@"AmericanTypewriter" size:16.0], NSFontAttributeName, nil];
[navigationBarAppearance setTitleTextAttributes:textTitleOptions];

This will change the font to "American TypeWriter".

But I have a custom font named "Pacifico", which is in my "Supporting Files" folder, as "Pacifico.ttf", and listed in my info.plist as "Fonts provided by application", but the font isn't changing, so somehow the custom font is not correctly referenced?

mrd
  • 4,561
  • 10
  • 54
  • 92
  • How are you initializing `navigationBarAppearance`in the first code snippet? And where is this code in your app delegate / view controller? – shim Jan 05 '15 at 16:44
  • `UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];` – mrd Jan 05 '15 at 16:46
  • The code is at the end of `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` – mrd Jan 05 '15 at 16:46
  • How are you initializing `myShadow` – shim Jan 05 '15 at 16:56
  • Also try a non-custom font / make sure the UIFont object is not nil. – shim Jan 05 '15 at 16:57
  • Have a look on this answer: http://stackoverflow.com/questions/7810563/how-do-you-use-settitletextattributesforstate-in-uibaritem/25382956#25382956 – Kampai Jan 06 '15 at 12:48

2 Answers2

1

For navigation bar title use

self.navigationController.navigationBar.titleTextAttributes= @{
   NSForegroundColorAttributeName: [UIColor purpleColor],
   NSFontAttributeName: [UIFont robotoMediumFontSize : 20.0],
};
self.navigationItem.title=@"MY Title";

For setting navigation bar background color use

self.navigationController.navigationBar.barTintColor=[UIColor myColor];

Logan
  • 52,262
  • 20
  • 99
  • 128
Amisha
  • 159
  • 1
  • 8
  • Yes ..You can write the following in Appdelegate – Amisha Jan 05 '15 at 18:54
  • To access navigation controller in appdelegate write `UINavigationController *navigationController =(UINavigationController *)self.window.rootViewController;` – Amisha Jan 05 '15 at 19:46
  • Then do `navigationController.navigationBar.titleTextAttributes=@{ NSForegroundColorAttributeName: [UIColor purpleColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:30.0], };` – Amisha Jan 05 '15 at 19:48
  • `navigationController.navigationBar.barTintColor=[UIColor myColor];` Then in each ViewController, write `self.title = @"Your navigation title";` now you will see all properties applied to this title and navigation bar – Amisha Jan 05 '15 at 19:49
0

I found the solution, the font needs to be added in the info.plist as described above, but additionally in ---> Build Phases ---> Copy Bundle Resources added.

mrd
  • 4,561
  • 10
  • 54
  • 92