简体   繁体   English

如何在iOS的TableView中的单元格内部的视图中添加阴影?

[英]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.

相关问题 如何在集合视图单元的内部底部边缘添加阴影? 迅捷3 - How to add a shadow to the inside bottom edge of a collection view cell? swift 3 如何在iOS 9.0或更高版本中将tableview单元格转换为带有阴影的圆角 - How to convert tableview cell to rounded corner with shadow in iOS 9.0 or later 如何在集合视图单元格的某一部分中添加多个数组,该集合视图位于tableView单元格内? - How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell? 如何向tableView单元格添加类似于Apple Health应用程序的渐变阴影? - How to add a gradient shadow to tableView cell similar to Apple Health app? 如何在tableview单元格内加载内容视图? - How to load content view inside a tableview cell? iOS如何有效地将UIView添加到tableview单元 - iOS how to add UIView to tableview cell effectively 如何将表格视图单元格添加到 xib 中的表格视图? - How to add table view cell to tableview in xib? 如何在uicollection视图中添加阴影? IOS - How to add shadow to uicollection view ? ios tableview单元格内的视图原点在ios中显示错误的框架 - origin of view inside the tableview cell shows wrong frame in ios ios-单元格tableview中的视图位置仅在滚动后才会更改 - ios - view position inside cell tableview changes only after scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM