_imageView1.layer.shadowOffset = CGSizeMake(0,0)//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
_imageView1.layer.shadowOpacity = 1//阴影透明度,默认0
_imageView1.layer.shadowRadius = 3//阴影半径,默认3
//路径阴影
UIBezierPath *path = [UIBezierPath bezierPath]
float width = _imageView1.bounds.size.width
float height = _imageView1.bounds.size.height
float x = _imageView1.bounds.origin.x
float y = _imageView1.bounds.origin.y
float addWH = 10
CGPoint topLeft = _imageView1.bounds.origin
CGPoint topMiddle = CGPointMake(x+(width/2),y-addWH)
CGPoint topRight = CGPointMake(x+width,y)
CGPoint rightMiddle = CGPointMake(x+width+addWH,y+(height/2))
CGPoint bottomRight = CGPointMake(x+width,y+height)
CGPoint bottomMiddle = CGPointMake(x+(width/2),y+height+addWH)
CGPoint bottomLeft = CGPointMake(x,y+height)
CGPoint leftMiddle = CGPointMake(x-addWH,y+(height/2))
[path moveToPoint:topLeft]
//添加四个二元曲线
[path addQuadCurveToPoint:topRight
controlPoint:topMiddle]
[path addQuadCurveToPoint:bottomRight
controlPoint:rightMiddle]
[path addQuadCurveToPoint:bottomLeft
controlPoint:bottomMiddle]
[path addQuadCurveToPoint:topLeft
controlPoint:leftMiddle]
//设置阴影路径
_imageView1.layer.shadowPath = path.CGPath
需要用下面的方式可以cell.layer.cornerRadius = 10
cell.contentView.layer.cornerRadius = 10.0f
cell.contentView.layer.borderWidth = 0.5f
cell.contentView.layer.borderColor = [UIColor clearColor].CGColor
cell.contentView.layer.masksToBounds = YES
cell.layer.shadowColor = [UIColor darkGrayColor].CGColor
cell.layer.shadowOffset = CGSizeMake(0, 0)
cell.layer.shadowRadius = 4.0f
cell.layer.shadowOpacity = 0.5f
cell.layer.masksToBounds = NO
cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath
为了改变单元格的背景色,你需要修改willDisplayCell:forRowAtIndexPath:方法,这在UITableViewCell documentation里提到过、所以你需要:- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{cell.backgroundColor = [UIColor redColor] }
contentView 仅仅是建议 subview 放入自定义控件,这样单元格能在表格编辑时适当的布局。
drekka
我发现this Cocoa With Love article 很有用,因为这看起来是一个完全定制的表视图,并且介绍了怎么做。
russes
还有一个简单的解决方案:当你在界面生成器里创建UITableViewCell的时候,仅仅拖拽一个额外的UIView,这样就能覆盖IB里创建的所有UITableViewCell。在额外添加的UIView的顶部放置额外添加的UI元素,你可以将额外的UIView设置成任何你想要的背景色。
StackOverflow上有很多答案建议你通过使用代码改变UITableViewCell的背景色,就像下面的这个:
cell.contentView.backgroundColor = [UIColor redColor]
如果你通过轻微调整contentView的框架大小来给单元格增加阴影,你或许会发现这行代码会同时改变单元格的背景色和阴影区域。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)