I want to add an image to the navigationBar. Ive used
[[UINavigationBar appearance] addSubview:logoImage];
it seems to work with iOS 7 but does not work with iOS 8, can anybody please explain why this is happening, and what is the alternate way to add an imageView on navbar in iOS 8?Answer1:
[[UINavigationBar appearance] returns an appearance proxy for the receiver class. The
addSubview: method is not tagged as <strong>
UI_APPEARANCE_SELECTOR</strong>. One major downside to UIAppearance's proxy approach is that it's difficult to know which selectors are compatible.
This article explains it a bit better: <a href="http://nshipster.com/uiappearance/" rel="nofollow">http://nshipster.com/uiappearance/</a>
And this link <a href="https://gist.github.com/mattt/5135521" rel="nofollow">https://gist.github.com/mattt/5135521</a> from the article, shows the methods that are tagged as <strong>
UI_APPEARANCE_SELECTOR</strong> in iOS 7.
You should subclass
UINavigationBar and call
[[UINavigationController alloc] initWithNavigationBarClass:toolBarClass:]. Pass your new navigation bar subclass as the first parameter and nil as the second parameter to use the standard
UIToolbar. In your subclass, add the image just like you are doing.
Another possibility that may work is getting the navigation bar of your navigation controller and adding the sub view there, like this:
[self.navigationController.navigationBar addSubview:logoImage]. This may work, but creating your own subclass will definitely give you more flexibility.