2

Hi i am working on iOS SDK5 with xcode 4.2 and i want to prepare a build which should be compatible with iOS 4.0 to 5.0 or at least 4.3 and 5.0

I have set the base sdk to 5.0 and deployment target to 4.0 but when i run the app in the iPhone 4 simulator my application crashes basically due to the code written below

[testNavigationBar setBackgroundImage:[UIImage imageNamed:@"facebook-navbar.png"] forBarMetrics:UIBarMetricsDefault];

but the above code runs fine when i switch the iPhone simulator from 4 to 5 also i have tried setBackgroundColor but its not working so kindly help me on this.

Radix
  • 3,639
  • 3
  • 31
  • 47

3 Answers3

2

The problem is that method was added to iOS5, so it doesn't exist in iOS4. You need to do a runtime check to see if you're in iOS5, like the following:

if ([testNavigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics)]) {
    [testNavigationBar setBackgroundImage:[UIImage imageNamed:@"facebook-navbar.png"] forBarMetrics:UIBarMetricsDefault];
}

Now the problem is that you won't get the background in iOS4, so you'll have to find a workaround.

Eduardo Scoz
  • 24,653
  • 6
  • 47
  • 62
1

That's because -setBackgroundImage: was introduced in iOS 5, and Apple don't (usually) back port new API to older platforms as a rule. Anyway, you have two options:

if ([testNavigationBar respondsToSelector:@selector(setBackgroundImage:)]) {
  // do what you're currently trying
}
else {
  //find a compatible way to do it
}

or:

//find a compatible way to do it
1

For iOS version 4.x, you can always override the UINavigationBar drawRect. Here is a posting on Stack Overflow on this very subject:

Background image for navigation view

Community
  • 1
  • 1
BP.
  • 10,033
  • 4
  • 34
  • 53