Question

I tried to insert a custom button in the center of UINavigationBar (in titleView). This still works in the first Level, but when I select in this TableView one cell and be pushed to the next controller, I'm not able to insert this button on the second controller too. The Right- and the LeftBarButtonItem are shown, but not the titleView.

When I'm working with the default settings and use viewController.title, it is shown in the titleView of this navigationBar, but my custom view is not shown.

My code in both controllers is

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];

I searched for that since more than one day, but nowhere I found that issue for the second level.

Thanks for your help.


Edit:

The solution is found by @Martin R in the first TableViewController i have to write

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];

In the second level:

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];

Question can be closed.

Thanks a lot! 25 minutes from writing the question to found a solution! Great time!

Was it helpful?

Solution

I think you have to set the titleView of the view controller's navigationItem:

self.navigationItem.titleView = centerNavigationButton;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top