【问题标题】:UIAppearance proxy problems with appearanceWhenContainedInUIAppearance 代理问题与appearanceWhenContainedIn
【发布时间】:2012-02-16 10:08:53
【问题描述】:

我正在尝试为背景视图中的每个标签添加阴影:

[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowOffset:CGSizeMake(0, -1)];

问题在于,在我的背景视图中有一些子视图(例如 tableview),哪些单元格的标签应该获得这个 shadowColor。

我这样做了:

[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowOffset:CGSizeMake(0, -1)];
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setShadowColor:[UIColor clearColor]];

但是文本阴影仍然存在于表格视图的单元格中。

谁能告诉我我做错了什么?!?

【问题讨论】:

    标签: ios shadow appearance


    【解决方案1】:

    您根本不能使用 UIAppearance 代理来自定义 UILabel。见this question。根据我的经验,尝试这样做会导致结果不一致和令人困惑。

    (我还看到了在 UILabel 上设置 appearanceWhenContainedIn:[somethingElse] 会导致所有其他 [UILabel appearance] 调用被忽略的问题)

    【讨论】:

    • 这很简单。当视图出现在屏幕上时,此处包含的所有 UILabel 都将符合此外观代理。由于 UIButtons 设置标题标签字体的方式,UIButtons 存在外观代理错误......但 UILabels 的功能和直截了当。 ...
    【解决方案2】:

    我会创建一个 UILabel 的子类并在其上设置阴影外观。

    【讨论】:

      【解决方案3】:

      我认为你有两个选择:

      1. 您可以将修改后的控件封装在自己的容器中并使用:

        @implementation ChildViewController
        
        - (void)viewDidLoad
        {
            [super viewDidLoad];
            [[UILabel appearanceWhenContainedIn:self.class, nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
            [[UILabel appearanceWhenContainedIn:self.class, nil] setShadowOffset:CGSizeMake(5.0, 5.0)];
        }
        
        @end
        

        更改将仅应用于托管在 ChildViewController 容器中的 UILabel 实例

      2. 或者您可以按照建议将 UILabel 子类化,以避免在当前容器中链接外观更改(因此单元格中的其他标签不受影响)。

      【讨论】:

      • 这在加载时确实没有意义。
      猜你喜欢
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      相关资源
      最近更新 更多