[英]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 嗨,我正在使用xcode 4.2开发iOS SDK5,我想准备一个应该与iOS 4.0至5.0或至少与4.3和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 我已将基本sdk设置为5.0,将部署目标设置为4.0,但是当我在iPhone 4模拟器中运行该应用程序时,由于以下代码,我的应用程序崩溃
[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. 但是当我将iPhone模拟器从4切换到5时,上面的代码运行良好,我也尝试过setBackgroundColor,但是它不能正常工作,请帮忙。
The problem is that method was added to iOS5, so it doesn't exist in iOS4. 问题在于该方法已添加到iOS5,因此在iOS4中不存在。 You need to do a runtime check to see if you're in iOS5, like the following: 您需要进行运行时检查,看看您是否在iOS5中,如下所示:
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. 现在的问题是,您不会在iOS4中获得背景,因此您必须找到一种解决方法。
That's because -setBackgroundImage: was introduced in iOS 5, and Apple don't (usually) back port new API to older platforms as a rule. 这是因为-setBackgroundImage:是iOS 5中引入的,而Apple通常不会(通常)将新API移植回旧平台。 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. 对于iOS版本4.x,您始终可以覆盖UINavigationBar drawRect。 Here is a posting on Stack Overflow on this very subject: 这是有关此主题的Stack Overflow上的帖子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.