2012-04-24 48 views
5

我正在製作iPhone應用程序,並且希望在導航欄的titleview位置顯示徽標。我做的UINavigationController的一個子類,並加入viewDidLoad中以下行:導航欄上的titleview在segues後的視圖中不可見

self.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]]; 

(這可能看起來像只有一條線路的浪費,但我使用它在多個地方,我不想由於各種原因使用[UINavigationBar外觀])

這適用於導航控制器的直接孩子 - 我看到了標誌。然而,當我將segue推到另一個視圖時,儘管它們(據我所知)是「相同」導航控制器的一部分,但徽標並未繼續存在。

(僅供參考) - > MyNavigationController - > SomeViewController - > SomeOtherViewController

我也試圖進入SomeOtherViewController的viewDidLoad中有:

self.navigationController.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]]; 

這仍然不顯示標誌。

經過一番打探,我發現,如果1)我沒有在navigationcontroller的viewDidLoad該行和2)我有第二行的SomeOtherViewController的viewDidLoad,那麼:

開始:查看SomeViewController 。沒有標誌。 點擊推動segue:標誌現在出現在SomeViewController的欄上,然後消失。 我現在正在查看SomeOtherViewController:它沒有徽標。 點擊返回:SomeViewController的導航欄仍然有徽標。

顯然我誤解了導航控制器的工作原理。誰能告訴我如何解決這個問題?

回答

1

如果我理解正確,你也可以用另一種方法做到這一點,只需用圖像替換整個NavigationBar?你可以放置你的圖標。

因爲改變整個NavigationBar是相當容易的。

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

你需要把在NavigationControllerviewDidLoad方法此代碼。然後,它也應該在ViewController中工作,你在NavigationController的堆棧上進行推送。

0

嘗試使用

self.navigationController.navigationBar.topItem.titleView = yourCustomView; 

self.navigationItem.titleView = yourCustomView; 

istead,不碰self.navigationController.navigationBar.topItem.titleView可言。起初我試圖將它們都設置爲yourCustomView,而我的自定義視圖出現了一瞬間,然後消失了我不知道在哪裏:)

相關問題