Search code examples
iphoneiosuiimageuiappearance

UINavigationBar background image does not change with rotation


I am trying to use a background image using the appearance methods in iOS 5. I’m setting the background images with these statements:

UIImage *portraitImage = [UIImage imageNamed:@"test_bar_portrait.png"];
UIImage *landscapeImage = [UIImage imageNamed:@"test_bar_landscape.png"];
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];

When I run the app, the correct image appears in portrait and landscape, but when I rotate back to portrait, it still has the landscape image shown.

I created a sample test application here: http://dl.dropbox.com/u/7834263/Appearance%20Test.zip

And screen shots of what is happening:

normal portrait

normal landscape

incorrect portrait


Solution

  • I tried changing your code to the following & it worked just fine for me.

    UIImage *portraitImage = [UIImage imageNamed:@"test_bar_portrait.png"];
    UIImage *landscapeImage = [UIImage imageNamed:@"test_bar_landscape.png"];
    [[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];
    
    self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    

    Please check it out.