2

I'm using SDWebImage to download images async into UICollectionView cells, but the downloaded images didn't fit the cell, where the cell only shows the upper corner of the image.

this is my code :

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCell

    cell.imageView.setShowActivityIndicator(true)
    cell.imageView.setIndicatorStyle(.gray)
    cell.imageView.sd_setImage(with: URL(string: imageURL[indexPath.row]))

    return cell
}

and this's my cellClass :

private class ImageCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)
    self.setupViews()
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.backgroundColor = .white
    imageView.layer.masksToBounds = true
    return imageView
}()

func setupViews() {
    backgroundColor = .white
    addSubview(imageView)
    imageView.contentMode = .scaleAspectFit
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.frame = CGRect(x: 0, y: 0, width: frame.width , height: frame.width )

}
}

I had followed this link Resize UICollectionView cells after image inside has been downloaded to solve it, but nothing changes.

So any ideas ?

Community
  • 1
  • 1
MEH
  • 1,777
  • 3
  • 15
  • 34

1 Answers1

2

If you don't want to use auto constraints you should remove this line

imageView.translatesAutoresizingMaskIntoConstraints = false

You should use this when you use auto constraints programmatically

abdullahselek
  • 7,893
  • 3
  • 50
  • 40
  • jazak allah kheer, (Thanks bro), could you please, explain more. – MEH Mar 09 '17 at 09:28
  • 1
    Take a look at these links [link](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/), [link](https://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2) I think enough to catch the point – abdullahselek Mar 09 '17 at 09:39
  • I appreciate it. – MEH Mar 09 '17 at 09:41