UINavigationBar appearance addSubview not working in iOS 8

China☆狼群 提交于 2019-12-12 04:52:28

问题


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?


回答1:


Calling [[UINavigationBar appearance] returns an appearance proxy for the receiver class. The addSubview: method is not tagged as UI_APPEARANCE_SELECTOR. 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: http://nshipster.com/uiappearance/ And this link https://gist.github.com/mattt/5135521 from the article, shows the methods that are tagged as UI_APPEARANCE_SELECTOR 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.



来源:https://stackoverflow.com/questions/26413782/uinavigationbar-appearance-addsubview-not-working-in-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!