0
votes

I am using following code to set the background image for the navigation bar for iPhone OS 5: [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tab bar.jpg"] forBarMetrics:UIBarMetricsDefault];

If I create the build for iPhone running OS Version 5...will it pose any problem if someone download the app and try to run on the device running OS version lesser 5?

Thanks

3

3 Answers

1
votes

The method setBackgroundImage:forBarMetrics: is not available on iOS 4 or lower. Thus calling this method will crash your app.

Just check if the object respons to selector:

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
   [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tab bar.jpg"] forBarMetrics:UIBarMetricsDefault];
}

Beware that the over ridding the drawRect: method used in iOS 4 and lower does not work in iOS 5. Thus you have to implement both ways to get your app working on iOS 4 and 5.

0
votes

According to the documentation:

 - (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
Available in **iOS 5.0 and later.**

So you should check for the current iOS version and on IOS 5 use:

 setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics.

and if you want to set the background in 4.0 here is the answer describing how to do it wither by adding a subview or using a category:

iPhone - NavigationBar Custom Background

0
votes

// Add the QuartzCore frameowrk in .h file

#import <QuartzCore/QuartzCore.h>

// If you want set different image for every view.Write this code in viewWillAppear method // NavigationBar background image.

if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)])
{
    //For iOS >= 5
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topbar.png"] forBarMetrics:UIBarMetricsDefault];
}
else {
    //For iOS < 5
    NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"topbar" ofType:@"png"];
    [self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
}