UINavigationBar:外观有效但不是UINavigationBar:appearanceWhenContained in [英] UINavigationBar:appearance works but not UINavigationBar:appearanceWhenContained in
问题描述
我需要将导航栏设置为自定义颜色,以下代码将执行此操作:
I have a requirement to set the navigation bar to a custom color and this following code will do that:
[[UINavigationBar appearance]
setBackgroundImage:navigationBarTileImage forBarMetrics:UIBarMetricsDefault];
然而,我的应用程序调用系统MFMailComposeViewController和MFMessageComposeViewController,我希望导航栏成为默认颜色那些观点,所以我这样做了:
However my application invokes the system MFMailComposeViewController and MFMessageComposeViewController and I want the navigation bar to be the default color for those views, so I did this:
[[UINavigationBar appearanceWhenContainedIn: [MyViewControllerBase class], [MyViewController1 class], [MyViewController2 class], nil]
setBackgroundImage:navigationBarTileImage forBarMetrics:UIBarMetricsDefault];
但是现在导航栏不再具有我的默认颜色。
为什么appearanceWhenContainedIn不工作?
However now the navigation bar no longer has my default color. Why is appearanceWhenContainedIn not working?
推荐答案
的参数appearanceWhenContainedIn:
表示视图(和/或视图控制器)包含层次结构,而不是可能容器的列表。 (不可否认,文档不是不清楚这一点。请参阅来自WWDC 2011的视频。)因此,
The argument to appearanceWhenContainedIn:
represents a view (and/or view controller) containment hierarchy, not a list of possible containers. (Admittedly, the docs aren't clear on this. See the video from WWDC 2011.) Thus,
[UINavigationBar appearanceWhenContainedIn:[NSArray arrayWithObjects:[MyViewControllerBase class], [MyViewController1 class], [MyViewController2 class], nil]]
为您提供 UINavigationBar $ c $的外观代理c>包含在
MyViewControllerBase
中,而 MyViewController1
位于 MyViewController2
。我猜这不是你拥有的包容层次结构。
gives you an appearance proxy for a UINavigationBar
contained within a MyViewControllerBase
, which in turn is within a MyViewController1
inside a MyViewController2
. I'm guessing that's not the containment hierarchy you have.
相反,请查看包含导航栏的视图控制器。它可能是一个通用的 UINavigationController
...但你不能只做
Instead, look at the view controller which contains your navigation bar. It's probably a generic UINavigationController
... but you can't just do
[UINavigationBar apperanceWhenContainedIn:[NSArray arrayWithObject:[UINavigationController class]]]
因为那样你就会获得邮件/消息控制器。遗憾的是,当你可以在邮件/邮件视图控制器中获取 UINavigationBar
的外观代理时,没有办法告诉你它可以撤消在更通用级别上进行的外观更改。
because then you'll get the mail/message controllers too. And sadly, while you can get at the appearance proxy for a UINavigationBar
within a mail/message view controller, there isn't a way to tell it to undo appearance changes made at a more generic level.
看起来这种情况的通常解决方案是让自己成为 UINavigationController
子类,并将其用于您想要换肤的UI部分。子类可以为空 - 它仅用于识别UI的部分 appearanceWhenContainedIn:
。 (同时, MFMailComposeViewController
之类的东西继续使用默认外观,因为它们仍然使用泛型 UINavigationController
。)
It looks like the usual solution to such scenarios is to make yourself a UINavigationController
subclass, and use it for the parts of your UI you want skinned. The subclass can be empty -- it only exists to identify parts of your UI for appearanceWhenContainedIn:
. (Meanwhile, things like MFMailComposeViewController
continue to use the default appearance because they still use the generic UINavigationController
.)
这篇关于UINavigationBar:外观有效但不是UINavigationBar:appearanceWhenContained in的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!