anc1revv
anc1revv

Reputation: 1623

Changing the color of a UINavigationBar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
{       
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                          target:self
                                                                                          action:@selector(cancel)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Send to Twitter"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(save)];

}
return self;
}

Another piece of code

 - (void)loadView 
    {
[super loadView];

self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor whiteColor];

self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.delegate = self;
textView.font = [UIFont systemFontOfSize:15];
textView.contentInset = UIEdgeInsetsMake(5,5,0,0);
textView.backgroundColor = [UIColor whiteColor];    
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:textView];
}

I'm trying to change the navigation bar color to black, however no matter what i do here, the color still stays default (Blue). How do you change the color of the bar on top of the view??

enter image description here

Upvotes: 0

Views: 2383

Answers (2)

Scott Forbes
Scott Forbes

Reputation: 7417

Try something like:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

This should be done on viewWillAppear: as during init method self.navigationController will be obtained as nil and hence will not produce any effect.

Hope this helps.

Upvotes: 6

Francesco Puglisi
Francesco Puglisi

Reputation: 2177

If you use a programatic approach (seems like you don't) you could write this code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

// window initialization
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

YourViewController *viewController = [[YourViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] \
initWithRootViewController:viewController];
[viewController release];
[[navigationController navigationBar] setTintColor:[UIColor blackColor]];
[window addSubview:navigationController.view];

[self.window makeKeyAndVisible];

return YES;

}

It works for me. Hope it helps! ;)

Upvotes: 0

Related Questions