0

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

devaditya
  • 321
  • 9
  • 21

3 Answers3

1

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.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Yes. You better check for the current iOS version before calling the method. --- Alternatively declare your App as compatible for iOS 5+ only. That prevents it from being installed on older OS. However, a quick check of the iOS version is not too difficult. – Hermann Klecker Nov 30 '11 at 12:36
  • 1
    Beter yet just check if the method is available with the `respondsToSelector` method. – rckoenes Nov 30 '11 at 12:37
0

// 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];
}
Dhara
  • 4,093
  • 2
  • 36
  • 69
abhi
  • 563
  • 6
  • 9
0

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

Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143