UIAppearance代理问题with appearanceWhenContainedIn

时间:2012-02-16 10:08:53

标签: ios shadow appearance

我试图给我的背景视图中的每个标签一个阴影:

[[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]];

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

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

3 个答案:

答案 0 :(得分:2)

您无法使用UIAppearance代理来自定义UILabel。见this question。根据我的经验,尝试这样做会导致不一致和混乱的结果。

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

答案 1 :(得分:1)

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

答案 2 :(得分:-2)

我认为你有两个选择:

  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,以避免在当前容器中链接外观更改(因此单元格中的其他标签不会受到影响)。