添加投影到自定义UITableCellView不起作用

时间:2013-12-19 04:48:49

标签: ios iphone cocoa-touch uitableview

所以我有一个自定义的UITableCellView,里面有一堆子视图。我正在尝试为此单元格添加阴影 -

-(void)drawRect:(CGRect)rect{
    [self.layer setShadowColor:[UIColor blackColor].CGColor];
    [self.layer setShadowOpacity:0.8];
    [self.layer setShadowRadius:3.0];
    [self.layer setShadowOffset:CGSizeMake(4.0, 4.0)];
    self.layer.masksToBounds = YES;


}

所以我覆盖了draw rect方法,但这不起作用。

我应该如何为每个单元格添加阴影?

3 个答案:

答案 0 :(得分:0)

cellForRowAtIndexPath

中试用此代码
cell.layer.shadowOffset = CGSizeMake(1, 0);
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowRadius = 5;
cell.layer.shadowOpacity = .25;


CGRect shadowFrame = cell.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
cell.layer.shadowPath = shadowPath;

答案 1 :(得分:0)

因为你有自定义单元格,所以你可以添加这样的阴影,


   //this is in custom cell assume u hav "CustomCell" class
   //in CustomCell.m 

  - (void)layoutSubviews
   {
       [super layoutSubviews];

       //add shadow properties by using layers 
        self.layer.shadowOpacity = 0.8f; //self is the custom cell 
        self.layer.shadowColor = [UIColor blackColor].CGColor; //set the shadow color

       // .. other layer properties and effects u want

   }


答案 2 :(得分:0)

试试这个

-(void)drawRect:(CGRect)rect
{
    [[self layer] setShadowColor:[[UIColor blackColor] CGColor]];
    [[self layer] setShadowOpacity:0.8f];
    [[self layer] setShadowRadius:3.0f];
    [[self layer] setShadowOffset:CGSizeMake(4.0f, 4.0f)];
    [[self layer] setShouldRasterize:YES];
    [[self layer] setRasterizationScale:[[UIScreen mainScreen] scale]];
}