QuickTip: Navigation Bar Image Fix in iOS 5

When working on a project, it was brought to my attention that the app’s custom navigation bar image wasn’t displaying. After looking at the code, the previous developer was using the drawRect: method to set the image. This no longer works in iOS 5 because the method never gets called, unless you subclass UINavigationBar. Below is a snippet of code to get the image to display on iOS 5 devices.

*UPDATE: Use run-time checking!*

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0) {
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"portrait_image.png"] forBarMetrics: UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"landscape_image.png"] forBarMetrics:UIBarMetricsLandscapePhone];
    [[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:201.0/255 green:31.0/255 blue:56.0/255 alpha:1]];
}
Email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>