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]]; }




#1 by Andrew on November 1, 2011 - 6:01 pm
Quote
so this is compile time checking? should we also do runtime checking of the feature availability? do we need both?
#2 by nick on November 1, 2011 - 6:08 pm
Quote
You’re correct, this is compile time. If you would like to do runtime checking something like this should work.
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0) {
// code here
}
#3 by Andrew on November 2, 2011 - 11:20 am
Quote
so do you need to so both? for this specific instance with custom nav bars, would only compile suffice?
#4 by nick on November 2, 2011 - 11:34 am
Quote
Just use the run-time, that is all you need.
#5 by swati on November 8, 2011 - 2:55 am
Quote
Thanks..
#6 by Tariq on January 16, 2012 - 7:10 am
Quote
many thanks to u…