【问题标题】:Adding drop shadow to custom UITableCellView not working向自定义 UITableCellView 添加阴影不起作用
【发布时间】:2013-12-19 04:48:49
【问题描述】:

所以我有一个自定义 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 方法来这样做 - 但这不起作用。

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

【问题讨论】:

  • 试试self.contentview.layer
  • 为什么是drawRect:?覆盖这种方法是昂贵的操作?尝试以下链接stackoverflow.com/questions/3546880/… 中提到的内容。希望对您有所帮助!
  • 等等 - 那我会覆盖什么?
  • contentview 的东西起作用了 - 但现在阴影只在一侧 - 我可以一直做到吗?
  • 投影会显示在视图的外部边界,UITableCellView 外部边界不显示在 UITableView 中

标签: ios iphone cocoa-touch uitableview


【解决方案1】:

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;

【讨论】:

    【解决方案2】:

    既然你有自定义单元格,那么你可以像这样添加阴影,


       //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
    
       }
    


    【讨论】:

      【解决方案3】:

      试试这个

      -(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]];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-19
        • 2015-11-03
        • 1970-01-01
        相关资源
        最近更新 更多