Objective c 无法仅在“中”设置导航栏上的图像;ios 5.0“;

Objective c 无法仅在“中”设置导航栏上的图像;ios 5.0“;,objective-c,ios,xcode,cocoa-touch,ios5,Objective C,Ios,Xcode,Cocoa Touch,Ios5,在我的通用应用程序中 我正在导航栏上设置图像…使用** UINavigationBar上的目标c类别 ** 该代码在iphone的ios 5.0中运行良好 *该代码在iPhone/iPad的ios 4.3中运行良好* 要在iOS 5中为导航栏设置BG图像,可以使用以下代码 if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){

在我的通用应用程序中

我正在导航栏上设置图像…使用**

UINavigationBar上的目标c类别

**

该代码在iphone的ios 5.0中运行良好 *该代码在iPhone/iPad的ios 4.3中运行良好*


要在iOS 5中为导航栏设置BG图像,可以使用以下代码

if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navigation.png"] forBarMetrics:UIBarMetricsDefault];
}
但请记住,这在iOS 4中不起作用。要使其在两者中都能工作,还需要向UINavigationBar添加一个类别

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"Navigation.png"];
[img drawInRect:rect];
}

如果您想设置后台应用程序范围,可以使用iOS5的UIAppearance

[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsLandscape];

如果同时使用这两个代码段,则不必查找iOs版本。如果你还想找到
[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsLandscape];