将自定义UIButton作为子视图的UITableViewCell:未显示按钮的CAGradientLayer

时间:2010-11-25 10:46:16

标签: uitableview uibutton calayer

在添加到UITableViewCell的contentView时,我的自定义UIButton子类出现了一些小问题。基本上,按钮类在其构造函数中添加CAGradientLayer以创建光泽背景:

CALayer *layer = self.layer;
layer.cornerRadius = self.frame.size.height/6;
layer.masksToBounds = YES;
layer.borderWidth = 1.0f;  
layer.borderColor = [UIColor colorWithRed:0.0 
                                  green:0.0 
                                   blue:0.0 
                                  alpha:.75].CGColor;    
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = [NSArray arrayWithObjects:
                      (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                      (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                      (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                      (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                      (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                      nil];
gradientLayer.locations = [NSArray arrayWithObjects:
                         [NSNumber numberWithFloat:0.0f],
                         [NSNumber numberWithFloat:0.5f],
                         [NSNumber numberWithFloat:0.5f],
                         [NSNumber numberWithFloat:0.8f],
                         [NSNumber numberWithFloat:1.0f],
                         nil];
gradientLayer.frame = self.layer.bounds;
[self.layer addSublayer:gradientLayer]; 

除了当我试图将此按钮添加为UITableViewCell的子视图时,这在应用程序的任何地方都很有效:

myButton = [[MyButton alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:oddsButton];

并在layoutSubviews中列出:

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect rect;
    rect.size.height = 30;
    rect.size.width = 60;
    rect.origin.x = self.frame.size.width - rect.size.width - 20;
    rect.origin.y = self.frame.size.height/2 - rect.size.height/2;
    myButton.frame = rect;
}

按钮显示正常,但图层根本没有显示 - 我只是得到一个矩形的纯色按钮。我也尝试在layoutSubviews中手动调整图层大小,但这没什么区别。这似乎表明在表视图单元格中忽略了所有石英绘图。作为替代方案,我考虑添加光泽作为按钮的子视图,这可以工作,但显示在按钮的普通视图/标签/等之上,所以看起来不太理想。

0 个答案:

没有答案