0

I'm trying set a cornerRadius and shadow of an UIImageVIew that's inside of a UICollectionViewCell. The problem is that I set clipToBounds = true and that stops the shadow and the cornerRadius.

I want to have cornerRadius, clipToBounds and shadow of the images. Not sure what I'd doing wrong here.

class PostPhotoCell: UICollectionViewCell {

@IBOutlet weak var selectedImage: UIImageView!
@IBOutlet weak var deleteButton: UIButton!


    override func awakeFromNib()
    {
    super.awakeFromNib()

    selectedImage.layer.cornerRadius = 5.0
    selectedImage.contentMode = .scaleAspectFill
    //applying overall shadow to image
    selectedImage.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor
    selectedImage.layer.shadowOffset = CGSize(width: 0, height: 0)
    selectedImage.layer.shadowOpacity = 1.0
    selectedImage.layer.shadowRadius = 6.0
    selectedImage.clipsToBounds = true
    }
}
Dani
  • 3,427
  • 3
  • 28
  • 54

3 Answers3

0

Try : selectedImage.layer.masksToBounds = true

check for the aspectMode and check clip subviews.

enter image description here

Ruhi
  • 176
  • 2
  • 16
0

You need to have a UIView as a shadow container and add your UIImageView to that. The following is an example of what you need to make shadow and corner radius work together with UIImageView.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let shadowView = UIView(frame: CGRect(x: 100, y: 100, width: 128, height: 128))
    shadowView.clipsToBounds = false
    shadowView.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor
    shadowView.layer.shadowOpacity = 1.0
    shadowView.layer.shadowOffset = CGSize.zero
    shadowView.layer.shadowRadius = 6.0
    shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: 6.0).cgPath
    view.addSubview(shadowView)

    let imageView = UIImageView()
    imageView.image = UIImage(named: "YOUR_IMAGE_NAME")
    imageView.frame = shadowView.bounds
    imageView.clipsToBounds = true
    imageView.layer.cornerRadius = 5
    shadowView.addSubview(imageView)
}
Lawliet
  • 3,438
  • 2
  • 17
  • 28
  • 1
    I get the cornerRadius, but no shadow. I tried `selectedImage.layoutIfNeeded()` and `self.layoutIfNeeded()` but still nothing. Just corner radius – Dani Jul 19 '17 at 11:13
  • Pls try again with my revised answer. This time I set `masksToBounds = false` – Lawliet Jul 19 '17 at 11:41
  • I tried. But it's not working. I get the shadow but not the `cornerRadius`. But from what I read, you cannot set cornerRadius and shadow on the same image view. It's more complicated than that – Dani Jul 19 '17 at 11:46
  • I updated my answer. It's already been tested. – Lawliet Jul 19 '17 at 13:06
0

If you are give corner Radius and shadow both on imageview, is not work. Try to use imageview inside view and then give corner radius on view and shadow on imageview

Vikash Kumar
  • 642
  • 1
  • 11
  • 25