2011-06-14 106 views
4

我用這些suggested solutions給我的UINavigationBar一個自定義圖像。 (爲了使它更加明確:我在AppDelegate.m文件中向UINavigationBar添加了一個類別)。 到目前爲止,這工作得很好,我沒有任何問題。但是,現在我正在最近的iOS5測試版上運行我的應用程序。 UINavigationBar現在再次爲空。UINavigationBar自定義背景圖像

由於我安裝的所有其他應用程序(使用自定義圖像)仍然表現相同,所以我的代碼中肯定有某些「錯誤」,我認爲iOS5現在不再支持。

因此,有沒有人有一個想法,我採用上述解決方案可能是什麼問題?

我發現使它工作的唯一方法是創建一個真正的UINavigationBar子類,然後在所有視圖中告訴IB使用該自定義類。不優雅,但...

+0

你有沒有找到你要找的解決方案?據我所知,iOS5需要使用setBackgroundImage:forBarMetrics:,但是你仍然可以在AppDelegate中的類別中使用它嗎?我發現的所有解決方案都要求我爲每個ViewController添加一些東西來支持iOS5,這正是我首先想要避免的。 – djibouti33 2011-10-19 06:00:12

+0

從迄今爲止的所有回覆看來,似乎並沒有像iOS 5那樣的唯一一個解決方案。因此,我認爲實現這一目標的唯一方法是將所有ViewController不是直接從UIViewController,而是從你自己的那個實現setBackgroundImage:forBarMetrics:默認情況下... – Dennis 2011-10-19 13:58:51

+0

看看這裏使用新的iOS5 API的UINavigationBar的[setBackgroundImage:forBarMetrics:](http:// stackoverflow。 com/questions/6283534/custom-background-image-on-uitoolbar -in-ios5-sdk/6343732#6343732) – loretoparisi 2011-06-14 13:00:39

回答

1

還有一個可能的「哈克」的解決方案是創建一個自定義視圖並將其插入到UINavigationBar的作爲一個子視圖 - 可它仍然可以工作:

UIView *backgroundView = ... 
[navigationBar insertSubview:backgroundView atIndex:0]; 

或檢查更新iOS5文檔中的UINavigationBar類參考設置自定義背景的內置方法

0

這爲我工作在我的工具欄

//toolBar background image set based on iOS version 
    [[UIDevice currentDevice] systemVersion]; 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) { 

     //iOS 5 
     UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"]; 

     if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
      [toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0]; 
     } 

    } else { 

     //iOS 4 
     [toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0]; 

    } 
+0

這會隱藏後退按鈕,以便在離開視圖並返回視圖時進行導航。 – 2012-02-24 20:11:10

1

使用此代碼

 UIImage *backgroundImage = [UIImage imageNamed:@"strip.png"]; 
     [upnavbar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; 

這個WIL工作。

+1

如果您可以在兩個問題上發佈相同的答案,請考慮將它們標記爲重複。 – ChrisF 2013-03-16 11:09:40