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

问题描述:

我知道这个问题看起来和其他人一样,但他们都没有提到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中模拟,它不工作,导航栏回到默认颜色..任何帮助?

谢谢。

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

在这里看到:UINavigationBar's drawRect is not called in iOS 5.0

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

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

我通常把它变成的appDelegate。

这里有一个类别,将双方的iOS 4和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