0

我试图在 UINavigationBar 的中心(在 titleView 中)插入一个自定义按钮。这仍然适用于第一级,但是当我在此 TableView 中选择一个单元格并被推送到下一个控制器时,我也无法在第二个控制器上插入此按钮。显示 Right- 和 LeftBarButtonItem,但不显示 titleView。

当我使用默认设置并使用 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];

我搜索了一天多,但没有找到第二级的问题。

谢谢你的帮助。


编辑:

@Martin 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 分钟!愉快的时光!

4

1 回答 1

0

我认为您必须设置titleView视图控制器的navigationItem

self.navigationItem.titleView = centerNavigationButton;
于 2012-09-12T11:32:14.473 回答