ios4中的导航栏自定义在ios5中不起作用

我知道这个问题看起来和其他人一样,但是他们都没有提到ios4和ios5之间的不兼容问题。

在我的应用程序中,我想自定义导航条的模式,但我的部署目标是ios4,所以我使用上面的代码执行appDelegate.m以下代码来执行此操作:

@implementation UINavigationBar (CustomImage) - (void)drawRect:(CGRect)rect { UIColor *color = [UIColor blackColor]; UIImage *image = [UIImage imageNamed: @"nav_bar.png"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; self.tintColor = color; } @end 

当我运行应用程序使用4.3模拟器,它工作正常,但是当我模拟ios5,它不工作,导航栏回到默认颜色..任何帮助?

谢谢。

我build议在iOS4上运行时使用您现有的代码,并使用新的iOS5function来自定义iOS5下的栏。

看到这里: UINavigationBar的drawRect不在iOS 5.0中调用

这是一个适用于iOS 4和iOS 5的类别:

 @implementation UINavigationBar (CustomBackground) - (UIImage *)barBackground { return [UIImage imageNamed:@"top-navigation-bar.png"]; } - (void)didMoveToSuperview { //iOS5 only if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { [self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault]; } } //this doesn't work on iOS5 but is needed for iOS4 and earlier - (void)drawRect:(CGRect)rect { //draw image [[self barBackground] drawInRect:rect]; } @end 

对于iOS5,您可以使用这种方法:

 if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {  [navigationBar setBackgroundImage:[UIImage imageNamed: @"nav_bar.png"]; forBarMetrics: UIBarMetricsDefault]; } 

我通常把它放到appDelegate中。