RyJ
RyJ

Reputation: 4025

iOS - Globally change navigation bar title color using appearance?

This crashes the app:

[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

Is there a way to do this using appearance?

Upvotes: 23

Views: 25936

Answers (7)

Sasan Soroush
Sasan Soroush

Reputation: 925

for iOS 15

let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <PREFERRED BACKGROUND COLOR>
appearance.titleTextAttributes = [.foregroundColor : <PREFERRED TITLE COLOR>]
navigationBar.tintColor = <PREFERED TINT COLOR> //for bar buttons
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance

Upvotes: 2

T Blank
T Blank

Reputation: 1418

Using modern syntax and code that actually runs, this is how to globally style your UINavigationBar title text:

NSShadow *navigationBarTitleShadow = [[NSShadow alloc] init];
navigationBarTitleShadow.shadowColor = [UIColor colorWithWhite:0.5
                                                         alpha:0.5];
navigationBarTitleShadow.shadowOffset = CGSizeMake(2.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor],
                                                        NSFontAttributeName : [UIFont fontWithName:@"Arial-BoldMT"
                                                                                              size:30.0],
                                                        NSShadowAttributeName : navigationBarTitleShadow }];

Note: NSShadow's shadowBlurRadius property is not respected.

Note: Shadows are so iOS 6. Don't ever use them.

Upvotes: 0

Iya
Iya

Reputation: 1918

Here's an example of how to do this in Swift:

UINavigationBar.appearance().titleTextAttributes =
  [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,
  NSForegroundColorAttributeName:UIColor.whiteColor()]

Upvotes: 4

Muhammad Nabeel Arif
Muhammad Nabeel Arif

Reputation: 19310

I used following code to change the title bar's color.

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 0);

NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                            NSShadowAttributeName:shadow};

[[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes];

Upvotes: 0

Max MacLeod
Max MacLeod

Reputation: 26652

The @RyJ answer is great and worked for me. Thought I'd chip in that there's a good tutorial on this in Ray Wenderlich's site, titled (excuse the pun):

User Interface Customization in iOS 6

See the section Customizing UINavigationBar

Here's the code snippet for the navigation bar title, to change globally:

// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
  UITextAttributeTextColor,
  [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
  UITextAttributeTextShadowColor,
  [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
  UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"Arial-Bold" size:0.0],
  UITextAttributeFont,
  nil]];

One other minor point is that it seems there's a default shadow on the title bar, so to get rid of it, you can't just remove the attribute. Instead you have to set a shadow offset:

UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]

Upvotes: 2

RyJ
RyJ

Reputation: 4025

This worked:

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];

Upvotes: 67

Ryan Poolos
Ryan Poolos

Reputation: 18551

That crashes the app before UINavigationBar doesn't have a title or state... Those are UIButton methods

You need

[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];

Upvotes: 3

Related Questions