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; 但你的版本是完全正确的! –