0

我試圖在UINavigationBar(在titleView中)插入一個自定義按鈕。這仍然在第一級,但是當我在這個TableView中選擇一個單元並被推送到下一個控制器時,我無法在第二個控制器上插入此按鈕。顯示RightBarButtonItem和LeftBarButtonItem,但不顯示titleView。UINavigationBar二級自定義標題視圖

當我使用默認設置並使用viewController.title時,它顯示在此導航欄的titleView中,但我的自定義視圖未顯示。

我在兩個控制器代碼

UIButton *centerNavigationButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[centerNavigationButton setBackgroundImage:[UIImage imageNamed: @"buttonImage.png"] forState:UIControlStateNormal]; 

centerNavigationButton.frame = (CGRect) { 
    .size.width = 100, 
    .size.height = 20, 
}; 
[centerNavigationButton addTarget:self action:@selector(scrollToTop) forControlEvents:UIControlEventTouchUpInside]; 
[self.navigationController.navigationBar.topItem setTitleView:centerNavigationButton]; 

我搜索的結果,因爲超過一天,但無處我發現這個問題的第二個層次。

感謝您的幫助。


編輯:

溶液通過@馬丁 - [R 在第一TableViewController我必須寫

UIButton *centerNavigationButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[centerNavigationButton setBackgroundImage:[UIImage imageNamed: @"buttonImage.png"] forState:UIControlStateNormal]; 

centerNavigationButton.frame = (CGRect) { 
    .size.width = 100, 
    .size.height = 20, 
}; 
[centerNavigationButton addTarget:self action:@selector(scrollToTop) forControlEvents:UIControlEventTouchUpInside]; 
[self.navigationController.navigationBar.topItem setTitleView:centerNavigationButton]; 

在第二級中發現:

UIButton *centerNavigationButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[centerNavigationButton setBackgroundImage:[UIImage imageNamed: @"buttonImage.png"] forState:UIControlStateNormal]; 

centerNavigationButton.frame = (CGRect) { 
    .size.width = 100, 
    .size.height = 20, 
}; 
[centerNavigationButton addTarget:self action:@selector(scrollToTop) forControlEvents:UIControlEventTouchUpInside]; 
[self.navigationItem setTitleView:centerNavigationButton]; 

問題可以關閉。

非常感謝!從寫這個問題開始25分鐘找到解決方案!好時光!

回答

0

我認爲你必須設置的titleView視圖控制器的navigationItem

self.navigationItem.titleView = centerNavigationButton; 
+0

我刪除了我最後的答案和編輯它: 非常感謝,這是它。我試圖使用 self.navigationController.navigationItem.titleView = centerNavigationButton; 但你的版本是完全正確的! –

相關問題