0

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

3 Answers 3

1

Try this in your first view controller:

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
2
  • I tried it but it doesn't work... And I've noticed an odd thing. In my opinion the navigationBar properties change but it is not repainted. Because I also have a little "Dark Info" button on my main view (that's the configuration you've by default when you created a new Utility Application), when I click on it, the main view flips to another one and when I come back on the main view the navigation Bar background has changed...
    – Rob
    May 25, 2012 at 11:27
  • I'm almost done with this problem ! I use the setNeedsDisplay method and it works. Almost. I'm just facing a little issue with my BackBarButton to redraw the initial navbar.
    – Rob
    May 25, 2012 at 12:24
1

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

1
  • Look at my Edit on my main post, thanks a lot for your answer !
    – Rob
    May 25, 2012 at 11:09
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.