2013-06-26 95 views
0

我有一個UINavigationController所有設置與我的初始視圖控制器,它允許用戶輸入一個值。它還有一個自定義的UILabel作爲titleView,這樣我就可以顯示一個完全「綠色」文本的「平面」導航欄。UINavigationController自定義LabelView時推新視圖控制器到堆棧

然後,當他們按下UIButton時,它將一個新的視圖推到堆棧上,這顯然根除了攜帶我的自定義titleView的UINavigationItem

我的問題是:我如何可靠地繼續爲導航欄的titleView使用自定義視圖?

我明明使用類delegate方法(willPushdidPush)試過了,但先明顯不具備topItem還,所以它最終加入我的UILabel到當前navigationItemdidPush存在視圖控制器啓動之間的可見延遲以及正在應用的新標籤titleView

我試了很多,但似乎沒有工作正常,我不推薦在推新視圖控制器時丟失動畫。

任何想法?

編輯:下面我的代碼發佈了一個粗略的大綱示例,它不是100%完美的,因爲它是對我在做什麼的概述,而不是直接從XCode粘貼。

// This code lives in the app delegate 

// Set up the root view controller that needs to be contained 
UIViewController *myFrontView = [[MYFrontViewController alloc] init]; 

// Set up the uinavigationcontroller 
UINavigationController *myNavController = [[MYNavigationController alloc] initWithRootViewController: myFrontView]; 

// Customise the initial UINavbar titleView: 
UILabel *myLabel = [[UILabel alloc] initWithText:@"My custom text"]; 

// Change the text Color. 
myLabel.textColor = [UIColor redColor]; 

// Set my new label as the custom titleview. 
[myNavController.navigationBar.topItem setTitleView: myLabel]; 

// Boilerplate code to make myNavController the rootViewController of the window etc. 

回答

0

那是因爲你設置UINavigationControllertitleView。這隻會暫時改變當前標題視圖。

要正確更改標題視圖,請使用UINavigationItemtitleView屬性。導航控制器框架將負責根據當前導航項目爲您設置UINavigationController的視圖,並確保動畫順利。

+0

我在navigationBar.topItem上設置titleView,它是'UINavigationItem'的一個實例,不是嗎? –

+0

你什麼時候這樣做?在大多數情況下,如果可能的話,最好的方法是在故事板中使用titleView插座。 – Krumelur

+0

我在代碼中構建了大部分視圖,並且在使用'initWithRootViewController:'方法'alloc視圖控制器'後立即設置titleView。 –

相關問題