1

I am using a collection view to display a collection of profile images and the person's name. So my cell has a UIImageView and a UILabel as subviews. I am using the UICollectionViewDelegateFlowLayout method:

collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

to calculate the cell's size depending on the available space.

All of this is working fine. I have added constraints to my subviews in the cell so that they also resize accordingly.

The issue I am running into is that I want my UIImageViews to be circles. It seems like auto layout is not recalculating the cell's subview size until after I have applied that effect. Instead, when I calculate the cornerRadius for the imageView it is still saying the imageViews width is 114.0 (which is what is in the storyboard) regardless of how big the cell is. This results in circles on the iPhone 5s but only rounded corners on any bigger device. Here is my code for that:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PersonCell", forIndexPath: indexPath)

    configureCell(cell, atIndexPath: indexPath)

    return cell
}

func configureCell(cell: UICollectionViewCell, atIndexPath indexPath: NSIndexPath) {
    if let person = personAtIndexPath(indexPath) {
        let imageView = cell.viewWithTag(100) as! UIImageView
        let nameLabel = cell.viewWithTag(200) as! UILabel

        cell.contentView.frame = cell.bounds
        cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        circularizeImageView(imageView)
        imageView.image = person.profileImage
        nameLabel.text = person.name


    }
}

func circularizeImageView(imageView: UIImageView) {
    imageView.layer.cornerRadius = (CGRectGetWidth(imageView.bounds) / 2)
    imageView.clipsToBounds = true
    imageView.layer.borderWidth = 2.0
    imageView.layer.borderColor = UIColor.whiteColor().CGColor
}

I saw in a couple places that previously the subviews would not resize at all like here: UICollectionView cell subviews do not resize

I don't think this is an issue any more, however I did add the fixes into my code as you can see in configureCell() but it still isn't helping.

So it seems like those subviews are not resized until after the cellForItemAtIndexPath call is completed. Any thoughts on how I might address this? See screenshot of :rounded corners on UIImageViews instead of complete circles

Community
  • 1
  • 1
Jadsada
  • 11
  • 3

1 Answers1

0

Don't use viewWithTag(), it's bad practice. Instead make the UILabel and UIImageView public or leave off the scope modifier.

Is the UIImageView a fixed size? If so, you don't need to call circularizeImageView() each time a cell is reused. Instead, call it in layoutSubviews() in your UITableViewCell subclass. This will also give you the correct size for imageView.bounds.height

bsmith11
  • 296
  • 1
  • 3
  • Thanks @bsmith11. That was my problem. I had't subclassed the UICollectionViewCell (which is why I was using the viewWithTag())...bad decision all around. Now I have subclassed the cell and call the cicularizeImageView() in layoutSubviews() for that subclass. It works perfectly and my code is much cleaner since my viewController is no longer responsible for the layout of the cells. – Jadsada Apr 27 '16 at 04:51