iOS:使用UIAppearance定义自定义UITableViewCell颜色 [英] iOS: Using UIAppearance to define custom UITableViewCell color
问题描述
如果我在分组的UITableViewCell上设置backgroundColor属性,则背景颜色会成功更改。很棒。
If I set the backgroundColor attribute on a grouped UITableViewCell, the background color successfully changes. Great.
但我想使用UIAppearance来改变我所有UITableViewCells的背景颜色,所以我可以在一个地方做到这一点,并影响到各处的变化。这是我的代码:
But I'd like to use UIAppearance to change the background color on all my UITableViewCells, so I can do it in one place and affect a change everywhere. Here's my code:
[[UITableViewCell appearance] setBackgroundColor:[UIColor colorWithRed:30.0/255.0 green:30.0/255.0 blue:30.0/255.0 alpha:1.0]];
UITableViewCell实现UIAppearance和UIAppearanceContainer,所以我认为这样可行。但事实并非如此。我也尝试过使用 - [UITableViewCell appearanceWhenContainedIn:(Class)]
,但这也不起作用。
UITableViewCell implements UIAppearance and UIAppearanceContainer, so I would have thought this would work. But it doesn't. I've also tried using -[UITableViewCell appearanceWhenContainedIn:(Class)]
, and that doesn't work either.
有什么想法吗?
推荐答案
更新(2013/7/8) - 这已在较新版本中修复iOS版。但是,如果你的目标是iOS 6或更低版本,那就值得了解。
你可以责怪苹果这个,它实际上是他们的意思。 技术上, backgroundColor
无法自定义通过外观代理。
You can blame Apple for this one, and it's actually pretty mean of them. Technically, backgroundColor
is not customizable through appearance proxies.
来自Apple的文档:
From Apple's documentation:
要支持外观自定义,类必须符合UIAppearanceContainer协议,并且必须使用UI_APPEARANCE_SELECTOR标记相关的访问器方法。
To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR.
如果我们进入像 UIBarButtonItem
这样的类,看看 tintColor
属性我们看到:
If we go into a class like UIBarButtonItem
and look at the tintColor
property we see this:
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
因为它标有 UI_APPEARANCE_SELECTOR
标签我们知道它适用于 UIAppearance
。
So because it's marked with the UI_APPEARANCE_SELECTOR
tag we know it works with UIAppearance
.
这是Apple特别吝啬的地方: UIView
, backgroundColor
没有外观选择器标记,但仍然可以使用 UIAppearance
即可。根据Apple提供的所有文档,它不应该,但它确实如此!
Here's where Apple are particularly mean: in a UIView
, backgroundColor
has no appearance selector tag, but still works with UIAppearance
. According to all the documentation Apple provide it should not, but yet it does!
这给人一种误导性的印象,它将适用于<$ c $的所有子类c> UIView ,包括 UITableView
。之前已经出现过,在之前的SO答案中
This gives the misleading impression that it will work for all sub-classes of UIView
, including UITableView
. This has come up before, in this previous SO answer
所以底线是 backgroundColor
根本不适用于 UIAppearance
,但出于某种原因,它确实在 UIView
上。它不能保证在 UIView
子类上工作,它在 UITableView
上根本不起作用。对不起,我无法给你一个更积极的答案!
So the bottom line is that backgroundColor
shouldn't work at all with UIAppearance
, but for some reason it does on a UIView
. It is not guaranteed to work on UIView
subclasses, and it doesn't work at all on UITableView
. Sorry I couldn't give you a more positive answer!
这篇关于iOS:使用UIAppearance定义自定义UITableViewCell颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!