[英]How to add shadow to a view inside cell in tableview in ios?
I have created a tableviewcell
in XIB and used it in tableview
using registering the class. 我已经在XIB中创建了一个
tableviewcell
,并通过注册该类在tableview
使用了它。 I am using autolayout
here. 我在这里使用自动
autolayout
。
But the problem is when i loading the tableview
in viewcontroller
the shadow of the view inside the cell is not setting correctly. 但是问题是当我在
viewcontroller
加载tableview
,单元格内部视图的阴影设置不正确。 it exceeds its bounds as i shown in picture with red box. 它超出了我在红色框中显示的图片范围。
But when is scrolling the tableview
then the shadow of the view is shown correctly as expected. 但是,当滚动
tableview
时,视图的阴影将按预期正确显示。
The code used in cellForRowAtIndexPath
is shown below: cellForRowAtIndexPath
使用的代码如下所示:
let layer = cell.cellContectView.layer
layer.shadowOffset = CGSizeMake(1.0, 1.0)
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowRadius = 5
layer.shadowOpacity = 0.2
layer.masksToBounds = false
layer.shadowPath = UIBezierPath(rect: layer.bounds).CGPath
Please note: the blurred text is not an issue. 请注意:文字模糊不成问题。 Its hidden for security reason
出于安全原因而隐藏
You can try to remove the code for the layer from the cellForRowAtIndexPath and put it in your cell class in: 您可以尝试从cellForRowAtIndexPath中删除该图层的代码,并将其放在您的单元格类中:
override func prepareForReuse() {
let layer = self.contentView.layer
layer.shadowOffset = CGSizeZero
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowRadius = 5
layer.shadowOpacity = 0.2
layer.masksToBounds = false
layer.shadowPath = UIBezierPath(rect: layer.bounds).CGPath
}
Adding a shadow inside UITableViewCell
using QuartzCore
code - can decrease scroll performance dramatically. 使用
QuartzCore
代码在UITableViewCell
添加阴影-会大大降低滚动性能。
Better to create shadow graphic asset and add it as UIImageView
. 最好创建阴影图形资产并将其添加为
UIImageView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.