2011-11-24 52 views
0

我知道這個問題看起來和其他人一樣,但他們都沒有提到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中模擬,它不工作,導航欄回到默認顏色..任何幫助?

謝謝。

回答

1

對於iOS5的,你可以使用這種方法:

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

我通常把它變成的appDelegate。

4

這裏有一個類別,將雙方的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