skip to Main Content

I am trying to create custom table view cell which works fine in my other UIViewControllers. However, in one of my controllers, the shadow is not growing, I can barely see the shadow.

Here is an image of the shadow being shown in red, you can see it is barely visible.

enter image description here

My cell has a UIView added inside the contentView to creating floating cell effects – the same code and same storyboard layouts are being used across my controllers but this is the only table view where the shadow issue is occurring – so I must be missing something.

My addShadow extension:

extension UIView {
    func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float) {
        layer.masksToBounds = false
        layer.shadowOffset = offset
        layer.shadowColor = color.cgColor
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
    }
}

My awakeFromNib on the custom cell:

:: cellContentView is my UIView added to the base contentView of the cell.

   override func awakeFromNib() {
        super.awakeFromNib()

        self.backgroundColor = .clear
        self.selectionStyle = .none
        cellContentView?.layer.masksToBounds = true
        cellContentView?.round(corners: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 10)
        cellContentView?.addShadow(offset: CGSize(width: 40, height: 60), color: UIColor.red, radius: 10, opacity: 1)
        cellContentView?.layer.shouldRasterize = true
    }

Note: The .round is an extension being used on all my cells.

No matter what radius or offset I add for this shadow, it does not get bigger than the image. Also, none of my other cells in the their controllers require the shouldRasterize property to be set, but this does.

Does anyone know what is happening here?
Thanks 🙂

Edit

Strangely, if I add constraints around my view to keep the gaps large between my view and the cell content view, the background colour disappears – this is set to white in the storyboard.

enter image description here

2

Answers


  1. You should call in the layoutSubviews method. because shadow should add after the view is uploaded

    override func awakeFromNib() {
        super.awakeFromNib()
        //init methods
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        //Added shadow
        self.reloadLayers()
    }
    
    private func reloadLayers() {
        self.layer.cornerRadius = 5
        self.addShadow(.TransactionCell)
    }
    

    I hope it helps

    Login or Signup to reply.
  2. Content view will fill you cell, so you need to add shadow to view inside content view which has all your components inside it. Then add constraints to it with gap between that view and content view. Second, 40 and 60 properties for shadow is likely too large, when I said too large I mean unbelievable large, because gap between content views in cells are no more than 15 – 30 even less. so try it with much less values, while radius can remain 10 but you will see what value fit the best. If cell content view is your custom view just values will did the job if your view is not kind of transparent or any inside it, in that case it won’t, and there is hard to fix that, I tried many libraries and custom codes and it is never ok.

        squircleView.layer.cornerRadius = 40
        squircleView.layer.cornerCurve = CALayerCornerCurve.continuous
        squircleView.layer.shadowColor = UIColor.systemGray.cgColor
        squircleView.layer.shadowOpacity = 0.7
        squircleView.layer.shadowOffset = CGSize(width: 0, height: 0.5)
        squircleView.layer.shadowRadius = 5
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search