18

I’m using the appearanceWhenContainedIn method on certain UI elements that I want to customise in my iOS 6 app. The problem I found is that none of my customisations are applied if I try to provide more than one container class, like so:

// Works neither for toolbar nor navbar items
[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIToolbar class], nil]

// Works fine (but only for navbar items, obviously)
[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]

The official docs say that the parameter for this method can be a nil-terminated list of multiple classes, but in my case it never works the way it should. Am I missing something here?

4

1 回答 1

43

从文档:

外观当包含在:

...

给定包含层次结构中接收器的外观代理。

这实际上意味着 nil-terminated list 定义的不是 UIBarButtonItem 的容器类列表,而是从上到下的容器层次结构,所以

[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIToolbar class], nil]

返回 UINavigationBar 内部的 UIBarButtonItem 的外观代理,而 UINavigationBar 又位于 UIToolbar 内部。

或者

[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class],[ViewController class], nil] setTintColor:[UIColor redColor]];

为 ViewController 类中的任何 UIToolBar 中的 UIBarButtonItems 设置红色色调。

因此,要分别设置 UINavigationBar 和 UIToolBar 的外观,您需要 2 次单独调用该+appearanceWhenContainedIn:方法

于 2012-09-25T22:44:08.873 回答