OGeek|极客世界-中国程序员成长平台

标题: ios - 层次结构中的 UIAppearance 实例 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:20
标题: ios - 层次结构中的 UIAppearance 实例

来自 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 的 实例之一,即对于 SomeXYZTableViewController 的两个不同实例,我想要不同的色调。

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



Best Answer-推荐答案


But only for one of the SomeXYZTableViewController's instances

这当然是可能的,但让我们采取一种更简单、更有可能的方法来做到这一点。你可以继承 UITableView。让我们调用子类 WhiteTableView。它什么也不做;它只是一个子类。您可以将其中一个 TableView 设为 WhiteTableView。

所以现在您可以指定表格 View 单元格应该具有白色色调,但前提是它们位于 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. 但工具栏中的条形按钮项是一个异常(exception):它们应该更白。

  3. 但是 DrillViewController View 中工具栏中的条形按钮项是异常(exception):它们应该是金色的。

关于ios - 层次结构中的 UIAppearance 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30339513/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4