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

Thanks!
Try the code you posted without the line:
self.cellBackgroundView.layer.shadowPath = UIBezierPath(rect: self.cellBackgroundView.bounds).cgPath
That should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With