'CollectionViewCell drop shadow with rounded corners

I've collectionViewCell and want to drop shadow to it, also want to have rounded corners, but it seems impossibile, cause to drop shadow clipToBounds must be set to false, with that, I can't add rounded corners, is there any kind of a "hack" to achieve my target?

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let countryCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MySubscribitionsCollectionViewCell
        countryCell.layer.cornerRadius = 10
        countryCell.layer.borderWidth = 1.0
        countryCell.layer.borderColor = UIColor.lightGray.cgColor
        countryCell.layer.backgroundColor = UIColor.white.cgColor
        countryCell.layer.shadowColor = UIColor.black.cgColor
        countryCell.layer.shadowOffset = CGSize(width: 2.0, height: 4.0)
        countryCell.layer.shadowRadius = 10.0
        countryCell.layer.shadowOpacity = 0.5
        countryCell.clipsToBounds = false
}


Solution 1:[1]

You can use another view to cast the shadow

Something like that

let shadowView = UIView()
shadowView.layer.masksToBounds = false
        // SET SHADOW
        
let mainView = UIView()
mainView.layer.cornerRadius = 10
mainView.layer.masksToBounds = true

shadowView.addSubview(mainView)

In the case of a cell, you can set the shadow on the cell itself, and set the rounding of the corners and your custom views on the contentView of collectionViewCell

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladislav