Rob
Rob

Reputation: 16002

When I change of UIView my NavigationBar doesn't

I have a ViewController embedded in a NavigationController whose I customize the NavigationBar (changing the background image in the code), and when I click on a button it leads the user to another view correctly. But, I can't change the image of the NavBar when I reach this view... I tried to write this :

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"otherBackground.png"] forBarMetrics:UIBarMetricsDefault];

In the IBAction method, in ViewDidUnload of my main ViewController and in ViewDidLoad and initWithNibName of my other ViewController, but nothing changes the previous image. What can I do ?

My IBAction is the following :

- (IBAction) versModeEmploi : (id) sender
{
    if(!modeEmploi) modeEmploi = [[ModeEmploiController alloc] initWithNibName:@"ModeEmploiController" bundle:nil];
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    [self.navigationController pushViewController:modeEmploi animated:NO];
}

Thanks a lot for any advice

Upvotes: 0

Views: 252

Answers (3)

Chris
Chris

Reputation: 3212

Try this in your first view controller:

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

Upvotes: 1

Rob
Rob

Reputation: 16002

Well if anyone is interested in the solution, I just had to use this to solve the problem :

[self.navigationController.navigationBar setNeedsDisplay];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav.png"] forBarMetrics:UIBarMetricsDefault];

This is to add in viewDidAppear/Disappear methods depending on what we want.

Upvotes: 1

Chris
Chris

Reputation: 3212

Very stupid question (aka, "did you try to turn it off and on again?"):

How did you transition from the "embedded view" to the second view? It should look something like this:

[self.navigationController pushViewController:detailViewController animated:YES];

If not, then self.navigationController won't point to anything usefull in your second view.

Regards, Chris

Upvotes: 1

Related Questions