//添加QuartzCore frameowrk在.h文件中
#import <QuartzCore/QuartzCore.h>
//如果要爲每個視圖設置不同的圖像,請在viewWillAppear
方法中寫入此代碼方法 // NavigationBar
背景圖像。
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];
}
是。在調用方法之前,您最好檢查當前的iOS版本。 ---另外聲明您的應用程序僅適用於iOS 5+。這可以防止它安裝在較舊的操作系統上。但是,快速檢查iOS版本並不困難。 –
尤爲明顯但只是檢查方法是可用的'respondsToSelector'方法。 – rckoenes