UINavigationBar外观addSubview在iOS 8中不起作用 [英] UINavigationBar appearance addSubview not working in iOS 8

查看:120
本文介绍了UINavigationBar外观addSubview在iOS 8中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像添加到navigationBar。我用过

I want to add an image to the navigationBar. Ive used

    [[UINavigationBar appearance] addSubview:logoImage];

它似乎适用于iOS 7但不适用于iOS 8,任何人都可以解释为什么这个正在发生什么,在iOS 8中在导航栏上添加imageView的替代方法是什么?

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?

推荐答案

调用 [[UINavigationBar appearance] 返回接收器类的外观代理。 addSubview:方法未标记为 UI_APPEARANCE_SELECTOR 。 UIAppearance的代理方法的一个主要缺点是很难知道哪些选择器兼容。

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.

本文解释得更好: http://nshipster.com/uiappearance/
此链接 https://gist.github.com/mattt/5135521 显示了标记为 UI_APPEARANCE_SELECTOR

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.

您应该继承 UINavigationBar 并致电 [[UINavigationController alloc] initWithNavigationBarClass:toolBarClass:] 。将新导航栏子类作为第一个参数传递,将nil作为第二个参数传递,以使用标准 UIToolbar 。在您的子类中,添加图像就像您正在做的那样。

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.

另一种可行方法是获取导航控制器的导航栏并在其中添加子视图,如下所示:
[ self.navigationController.navigationBar addSubview:logoImage] 。这可能有用,但创建自己的子类肯定会给你更多的灵活性。

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.

这篇关于UINavigationBar外观addSubview在iOS 8中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆