1

I'm trying to change my navbar's background with the code:

- (void)viewDidLoad
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"header_bg_smooth.png"] forBarMetrics:UIBarMetricsDefault];

    [super viewDidLoad];
...

}

It works, but the problem I'm having is that the change first takes effect when you go BACK the the view where this code is located. So it you go to this view the navbar is not changed but if you go further and then go back to this view the changes take effect.

Does anyone have any clue what the issue might be?

Thanks in advance

PaperThick
  • 2,749
  • 4
  • 24
  • 42
  • keep this line [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"header_bg_smooth.png"] forBarMetrics:UIBarMetricsDefault]; in appdelgate didfinishlaunching method and see how it is coming.... – iSpark Apr 24 '13 at 07:51

4 Answers4

2
 - (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg_smooth.png"] forBarMetrics:UIBarMetricsDefault];
...

}
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • this maybe correct. Why maybe? It is because `self.navigationController.navigationBar` does not necessarily have values in it. It may be `nil` and if so, your code will crash. – Raptor Apr 24 '13 at 07:41
  • 2
    @BurhanuddinSunelwala This worked Thank you so much! Take in to consideration what Shivan Raptor wrote adn make sure that the navbar is there – PaperThick Apr 24 '13 at 07:54
1

viewDidLoad only fired once when the view is loaded. To run the code every time when you see the view, put the codes in viewDidAppear or viewWillAppear (depends on your usage)

This answer helps you choose from viewDidLoad , viewDidAppear or viewWillAppear.

Community
  • 1
  • 1
Raptor
  • 53,206
  • 45
  • 230
  • 366
0

Use setBackgroundImage:forBarMetrics: method: and write this method in viewWillAppear

[navbar setBackgroundImage:[UIImage imageNamed:@"navbar"] 
               forBarMetrics:UIBarMetricsDefault];

this will help you a lot....

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
0

try this..

UIView *backgroundView = ...
[navigationBar insertSubview:backgroundView atIndex:0];

Also see this link....setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70