Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-resizing UITableViewCell shadow is off

I have a custom UITableViewCell with a self sizing UILabel and a shadow.

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

I've tried to implement this code in layoutSubviews(), cellForRowAt: indexPath and willDisplay cell but it is always off and not centered:

Shadow is not centered but off What's the correct way to implement it?

Edit: It works when I start scrolling in the tableView.

Edit 2: This is how it should look like:

This is how it should look!

Thanks!

like image 381
kuklis Avatar asked Feb 01 '26 21:02

kuklis


1 Answers

Try the code you posted without the line:

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

That should do the trick.

like image 139
DonMag Avatar answered Feb 03 '26 10:02

DonMag