简体   繁体   中英

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?

不要使用self.parentViewController ,而是self

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

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM