0

My UIImageView doesn't appear rounded util scrolling or reload the collection view (collectionView.reloadData()).

This is how it looks:

  • Without rounding

enter image description here

  • With rounding

enter image description here

The cell has multiple image views and all of them are constrained to go with facesContainer UIView constrains.

I tried to implement this code is inside collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) :

cell.facesContainer.layer.cornerRadius = (cell.facesContainer.layer.frame.width/2)
cell.facesGroupImageView.layer.cornerRadius = (cell.facesGroupImageView.layer.frame.width/2)
cell.facesGroupImageView.layer.masksToBounds = true
cell.facesGroupImageView.layer.borderColor = UIColor.black.cgColor
cell.facesGroupImageView.layer.borderWidth = 0.5

Also, I tried to add the code snippet above in the custom cell class, at awakeFromNib() method, but it doesn't work.

Why it doesn't work? how to fix it?

Krunal
  • 77,632
  • 48
  • 245
  • 261

2 Answers2

2

I think you should put your codes inside this:

DispatchQueue.main.async {
   // your view configurations
}
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69
1

Setting radius in viewWillLayoutSubviews will solve the problem.

override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() profileImageView.layer.cornerRadius = profileImageView.frame.height / 2.0 }

Sajid Zeb
  • 1,806
  • 18
  • 32