I am making an iPhone application, and would like to display a logo in the titleview position of my navigation bar. I've made a subclass of UINavigationController and added the following line in viewDidLoad:
self.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];
(this may seem like a waste for just one line, but i'm using it in multiple places and I don't want to use [UINavigationBar appearance] for various reasons)
This works fine for the immediate child of the navigation controller - I see the logo. However, when I push segue to another view, the logo does not carry over, even though they are (to the best of my knowledge) part of the "same" navigation controller.
(for reference) -> MyNavigationController -> SomeViewController -> SomeOtherViewController
I have also tried going into the viewDidLoad of SomeOtherViewController with a:
self.navigationController.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];
This still does not show the logo.
After some poking around, I have discovered that if 1) I do not have that line in the navigationcontroller's viewDidLoad and 2) I do have the second line in the SomeOtherViewController's viewDidLoad, then:
Start: Viewing SomeViewController. No logo. Click to push segue: The logo now appears on SomeViewController's bar, before disappearing. I am now viewing SomeOtherViewController: which has no logo. Click back: SomeViewController's navigation bar still has the logo.
Obviously I am misunderstanding something about how a navigationcontroller works. Can anyone tell me how to fix this?
If I understand it right you could also do it with another approach and just replace the whole NavigationBar with an image? There you could place your icon.
Because changing the whole NavigationBar is quite easy.
if ([self.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]) {
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
}
This code you need to put in the viewDidLoad
method of the NavigationController. Then it should also be working in ViewControllers you push on the stack of the NavigationController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With