2012-04-15 49 views
2

我改變UINavigationBar背景圖片重載方法更改UINavigationBar的背景圖片爲默認 - 的iOS 4.3

@implementation UINavigationBar (Background) 
-(void) drawRect:(CGRect)rect 
{ 
    UIImage *image = [UIImage imageNamed:@"Header.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 

@end 

但在一些UIViewControllers我需要刪除此背景圖像。我如何做到這一點?

+0

你是否繼承UINavigationBar? – Otium 2012-04-15 14:55:32

+0

不,我更新了我的代碼。 – 2012-04-15 14:57:13

回答

2

你可能看this blog post from Sebastian Celis。它解決了你面臨的問題。

也許還檢查this

+0

不,他解釋瞭如何實現UINavigationBar子類以及如何在橫向和默認模式下更改UINavigationBar圖像。我知道如何做到這一點。我需要將UINavigationBar返回到UIBarStyleDefault。簡單的方法:\t self.navigationController.navigationBar.barStyle = UIBarStyleDefault; 沒有幫助 – 2012-04-15 16:06:19

+0

您需要繼承UINavigationBar,而不是創建它的類別 – Otium 2012-04-15 16:09:07

0

測試這樣

+ (void)setNavigationbarBackground:(UINavigationBar *)nBar imageName:(NSString *)imgName { 
    if([nBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
     [nBar setBackgroundImage:[UIImage imageNamed:imgName] forBarMetrics:UIBarMetricsDefault]; 
    } else { 
     [nBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:imgName]]]; 
    } 
} 
+0

我知道如何自定義我的NavigationBar。我需要扭轉這個動作 – 2012-04-15 15:01:54

+0

哎呀我錯了,對不起 – WhiteTiger 2012-04-15 15:03:21

+2

這種測試,對我有用。 [nBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; – WhiteTiger 2012-04-15 15:15:49