Anas Almomany
Anas Almomany

Reputation: 9

Why UIImageView doesn't display rounded until scrolling?

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

This is how it looks:

enter image description here

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?

Upvotes: 0

Views: 706

Answers (2)

Sajid Zeb
Sajid Zeb

Reputation: 1978

Setting radius in viewWillLayoutSubviews will solve the problem.

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

Upvotes: 1

Ahmadreza
Ahmadreza

Reputation: 7212

I think you should put your codes inside this:

DispatchQueue.main.async {
   // your view configurations
}

Upvotes: 2

Related Questions