ios - 自动调整 UITableViewCell 阴影大小已关闭

标签 ios swift uitableview

我有一个自定义的 UITableViewCell,带有一个自调整大小的 UILabel 和一个阴影。

class CustomCell: UITableViewCell {

  @IBOutlet weak var titleLabel: UILabel!
  @IBOutlet weak var cellBackgroundView: UIView!
  @IBOutlet weak var creationDateLabel: UILabel!

  override func layoutSubviews() {
    super.layoutSubviews()
    self.cellBackgroundView.layer.cornerRadius = 12

    self.cellBackgroundView.layer.shadowColor = UIColor.black.withAlphaComponent(1.0).cgColor
    self.cellBackgroundView.layer.shadowOffset = CGSize.zero
    self.cellBackgroundView.layer.shadowRadius = 9
    self.cellBackgroundView.layer.shadowOpacity = 0.8
    self.cellBackgroundView.layer.shadowPath = UIBezierPath(rect: self.cellBackgroundView.bounds).cgPath
    self.cellBackgroundView.layer.masksToBounds = false
    self.cellBackgroundView.layer.shouldRasterize = true

    self.cellBackgroundView.layer.rasterizationScale = UIScreen.main.scale
  }
}

我尝试在 layoutSubviews()cellForRowAt: indexPathwillDisplay cell 中实现此代码,但它始终处于关闭状态居中:

Shadow is not centered but off 什么是正确的实现方式?

编辑:当我开始在 tableView 中滚动时它会起作用。

编辑 2: 它应该是这样的:

This is how it should look!

谢谢!

最佳答案

试试你发布的代码没有这一行:

self.cellBackgroundView.layer.shadowPath = UIBezierPath(rect: self.cellBackgroundView.bounds).cgPath

这应该可以解决问题。

关于ios - 自动调整 UITableViewCell 阴影大小已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372422/

相关文章:

ios - 运行时找不到方法

ios - 如何限制 UITextField 中的小数输入值

ios - 以编程方式通过单元格设计重用错误 tableView

ios - Autolayout 或使用 NSAttributedString 计算高度来实现 UITableViewCell 的动态高度,哪种方法最好?

ios - 在 tableView.reloadData() 之后,通用函数不会更新 tableview 单元格中标签的值

ios - 如何以编程方式将不同单元格的不同图像添加到 tableView (Swift 3)

android - 如何跟踪从 Unity 应用程序发送的 Facebook appRequest?

iOS 使用应用商店证书退出企业 IPA

swift - 在主项目中找不到链接到 Cocoapod 的静态框架

ios - 如何获取尺寸并加载PDF页面?