iPhone构建使用的Xcode 4.2,它支持4.0版本到5.0
问题描述:
你好我工作在iOS SDK5和Xcode 4.2,我要准备哪些应该与iOS 4.0兼容到5.0,或者至少4.3和5.0iPhone构建使用的Xcode 4.2,它支持4.0版本到5.0
构建我已基SDK设置为5.0和部署目标〜4.0,但是当我运行在iPhone 4模拟器应用程序我的应用程序崩溃基本上是由于下面写
[testNavigationBar setBackgroundImage:[UIImage imageNamed:@"facebook-navbar.png"] forBarMetrics:UIBarMetricsDefault];
但代码上面的代码运行正常时i开关从4到5 iPhone模拟器也我已经尝试setBackgroundColor,但它不工作,如此善良帮助我。
答
这是因为-setBackgroundImage:是在iOS 5中引入的,Apple通常不会(通常)将新API作为规则返回到旧平台。无论如何,你有两个选择:
if ([testNavigationBar respondsToSelector:@selector(setBackgroundImage:)]) {
// do what you're currently trying
}
else {
//find a compatible way to do it
}
或:
//find a compatible way to do it
答
问题是该方法被添加到iOS5,所以它不存在于iOS4。你需要做一个运行时检查,看是否你在iOS5中,如下所示:
if ([testNavigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics)]) {
[testNavigationBar setBackgroundImage:[UIImage imageNamed:@"facebook-navbar.png"] forBarMetrics:UIBarMetricsDefault];
}
现在的问题是,你不会得到在iOS4的后台,所以你必须找到一个解决方法。
答
对于iOS 4.x版,你总是可以覆盖UINavigationBar的drawRect中。这里是关于这个主题的Stack Overflow上的贴子: