2
votes

I ran into this issue in iOS 8 where the wrong background image for the navigation bar is displayed in any of these situations:

  • the view controller-based status bar appearance is set to YES (default) and the navigation controller is presented modally.
  • the view controller-based status bar appearance is set to NO and the status bar is initially hidden. In this case, the navigation controller does not need to be presented modally to display the wrong image.

To isolate the problem where the view controller-based status bar appearance is set to YES (default) and the navigation controller is presented modally I created a test project from scratch following these steps:

  1. Create a new project with the "Master-Detail Application" template.

  2. Open Main.storyboard and add a Navigation Controller to it. Remove its root view controller and connect the Master View Controller with a modal segue. Then connect the Detail View Controller as its root view controller. You should end up with something like this:

enter image description here

  1. Customise the navigation bar background via the appearance proxy. Use two different images for portrait (UIBarMetricsDefault) and landscape (UIBarMetricsCompact). I used a category on UIImage to create the images from solid colors.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor blueColor]] forBarMetrics:UIBarMetricsCompact];
    
        return YES;
    }
    
  2. Run the app. The Master view controller displays the right navigation bar background images for portrait and landscape orientations:

enter image description here

enter image description here

  1. Now present the Detail view controller modally. To do so, tap on the "Add" button and then select the newly created row. The Detail view controller displays the right navigation bar background image.

  2. Rotate the interface. The Detail view controller will not change the navigation bar background image (wrong):

enter image description here

  1. Rotate the interface back to its original orientation. The Detail view controller will change the navigation bar background image (wrong):

enter image description here

Has anyone else struggled with this?

1

1 Answers

2
votes

Yes,I have been struggling with this, after I add "View controller-based status bar appearance" in target's plist file, it is working again on iOS 8, remember to set it to NO.

In MasterViewController, add this to avoid wrong background image when detailViewController is dismissed. It basically set the same appearance again.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor blueColor]] forBarMetrics:UIBarMetricsCompact];
}