简体   繁体   中英

iPhone build which supports version 4.0 to 5.0 using Xcode 4.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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM