I'd like to know the best way to resize a UICollectionViewCell's dimensions dynamically(Swift 3). I have asynchronous image uploading to a ViewController which implements the appropriate protocols for UICollectionView. The images will all be different sizes, so I will need to set the cell dimensions after loading the UIImageView data. I'm open to options for the layout of the collectionview itself, but first I want to get the cell dimensions correct.
I know there are a few posts already that are similar, but I'd like to know the most appropriate way to do this in Swift 3 and I can't quite find the right way yet.
Is it possible to do the resizing dynamically in the func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {}
method? Or, is there an autoresize option I can set when loading the whole view?
Here's my relevant methods at the moment, all static sizing of the CollectionView:
override func viewDidLoad() {
super.viewDidLoad()
ExitNameLabel.text = exitName
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 120 , height: 120)
self.imagesCollectionView.setCollectionViewLayout(layout, animated: true)
exitLocationDetails.text = exitLocation
imagesCollectionView.delegate = self
imagesCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return imageUrls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:ImageCollectionViewCell = imagesCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
if (images_cache[imageUrls[indexPath.row]] != nil) {
cell.imageCell.image = images_cache[imageUrls[indexPath.row]]
} else {
loadImage(imageUrls[indexPath.row], imageview:cell.imageCell)
}
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
let width = screenSize.width
return CGSize(width: CGFloat(width), height: imagesCollectionView.bounds.size.height)
}
Thanks for helping out an iOS newbie :)