user869305
user869305

Reputation: 151

Change NavigationBar color (background color)

I keep reading to change the default color on a navigationbar i just need to update the first method in the appdelegate to this

self.window.rootViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

but it doesn't seem to work, so I tried setting it in the viewDidLoad method of the firstview also:

self.parentViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

This didn't work either. How can I change this?

Upvotes: 6

Views: 6971

Answers (4)

Dalorzo
Dalorzo

Reputation: 20024

In IOS7 you can try this:

[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

You can follow these steps:

I created a new UINavigationController for example UIDemoNavController resulting in:

- (void)viewDidLoad{
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];  
    [super viewDidLoad];
}

This is the full demo class:

#import "UIDemoNavController.h"

@interface UIDemoNavController()

@end

@implementation UIDemoNavController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {}
    return self;
}

- (void)viewDidLoad{
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

@end

Upvotes: 1

Isuru Jayathissa
Isuru Jayathissa

Reputation: 488

When iphone 5 come we have to set both device type. So use this

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    //iOS 5 new UINavigationBar custom background
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}

Upvotes: 1

DrummerB
DrummerB

Reputation: 40221

Don't use self.parentViewController, but self:

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

Upvotes: 6

graver
graver

Reputation: 15213

Try self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewController class has a property navigationController, which returns the navigation controller its embedded in no matter how deep, otherwise it returns nil.

Upvotes: 0

Related Questions