Modifying layer properties for contents of collection view cell

时间:2015-07-31 19:56:09

标签: ios swift uiimageview uicollectionview

I'm having mixed results when modifying the layer properties of an UIImageView. In a UICollectionView, I have UICollectionViewCells that have a single UIImageView in them. I want those images to look like circles, thus, in my UICollectionViewCell subclass, I override layoutSubviews and placed my code there, like so:

override func layoutSubviews() {
  super.layoutSubviews()
  image.layer.cornerRadius = CGRectGetWidth(image.bounds) / 2
  image.layer.masksToBounds = true
}

However, I was getting unreliable results. Sometimes, the images are perfect circles (as intended), but sometimes they're squares with rounded edges, that may or may not become circles if you scroll through the collectionView.

Where is the best place to put this code?

1 个答案:

答案 0 :(得分:0)

It depends on the size of the image. You need to have square images to create circles using cornerRadius. You can achieve this by setting the width constraint for the image and then setting the aspect ratio to 1:1.