层次结构中的 UIAppearance 实例

UIAppearance instances in hierarchy

来自 Apple 关于 UIAppearance 的文档:

To customize the appearances for instances of a class contained within an instance of a container class, or instances in a hierarchy, use +appearanceWhenContainedIn: for the appropriate appearance proxy.

In any given view hierarchy the outermost appearance proxy wins. Specificity (depth of the chain) is the tie-breaker.

In other words, the containment statement is treated as a partial ordering. Given a concrete ordering (actual subview hierarchy), we select the partial ordering that is the first unique match when reading the actual hierarchy from the window down.

有人可以举例说明可以指定层次结构的情况吗?

考虑我希望仅针对特定 UITableViewController 子类的实例设置 UITableViewCells 样式的情况

[[UITableViewCell appearance] setTintColor:[UIColor whiteColor]];

但仅针对 SomeXYZTableViewController's 个实例之一,即 SomeXYZTableViewController 的两个不同实例,我想要不同的色调。

还有很多其他方法可以做到这一点,但我只是想知道是否可行。

But only for one of the SomeXYZTableViewController's instances

这当然是可能的,但让我们采用一种更简单、更可行的方法来做到这一点。您可以将 UITableView 子类化。让我们调用子类 WhiteTableView。它什么都不做;它只是一个子类。您可以将 table 视图之一设为 WhiteTableView。

现在您可以指定 table 视图单元格应具有白色调颜色,但仅当它们位于 WhiteTableView 内时。

我在书中给出的例子——来自现实生活——是:

[[UIBarButtonItem appearance]
    setTintColor: [UIColor myGolden]];
[[UIBarButtonItem appearanceWhenContainedIn:
    [UIToolbar class], nil]
        setTintColor: [UIColor myPaler]];
[[UIBarButtonItem appearanceWhenContainedIn:
    [UIToolbar class], [DrillViewController class], nil]
        setTintColor: [UIColor myGolden]];

这意味着:

  1. 一般来说,条形按钮项目应该是金色的。

  2. 但工具栏中的条形按钮项目是个例外:它们应该更浅。

  3. 但是DrillViewController视图中工具栏中的条形按钮项是例外:它们应该是金色的。