Reputation: 28572
I ran into this issue in iOS 8 where the wrong background image for the navigation bar is displayed in any of these situations:
YES
(default) and the navigation controller is presented modally.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:
Create a new project with the "Master-Detail Application" template.
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:
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;
}
Run the app. The Master view controller displays the right navigation bar background images for portrait and landscape orientations:
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.
Rotate the interface. The Detail view controller will not change the navigation bar background image (wrong):
Has anyone else struggled with this?
Upvotes: 2
Views: 1009
Reputation: 13766
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];
}
Upvotes: 2