仅在“ios 5.0”中才能在导航栏上设置图像

在我的通用应用中,

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

objective c category...on UINavigationBar

**

代码在iphone中的ios 5.0中 正常工作 * 代码在iPhone / iPad的ios 4.3中正常工作 *

 *But not working in **ios 5.0 iPad*** - (void) drawRect:(CGRect)rect { UIImage *image; image = [UIImage imageNamed: @"Navigation.png"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; [self setTintColor:[UIColor clearColor]]; NSLog(@"Draw Rect"); } 

要在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];