I'm trying to create a collection view that can be resized automatically from label height. I have two different cell, one of them with photo another without photo and both in xib files.
I've tried different ways to make this height dynamic but no solution ((.
var CollectionViewCellNib = UINib(nibName: "CollectionViewCell", bundle: nil)
collectionView.registerNib(CollectionViewCellNib, forCellWithReuseIdentifier: "CollectionViewCell")
var CollectionViewCellWithPhotoNib = UINib(nibName: "CollectionViewCellWithPhoto", bundle: nil)
collectionView.registerNib(CollectionViewCellWithPhotoNib, forCellWithReuseIdentifier: "CollectionViewCell")
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let pikirler = self.pikirler[indexPath.row]
let targetWidth: CGFloat = (self.collectionView.bounds.width - 2 * kHorizontalInsets)
if indexPath.row == 2 {
var cell = self.offscreenCells[CollectionViewKeyWords.collectionViewOnlyTextCell] as? CollectionViewCell
if cell == nil {
cell = NSBundle.mainBundle().loadNibNamed("CollectionViewCell", owner: self, options: nil)[0] as? CollectionViewCell
self.offscreenCells[CollectionViewKeyWords.collectionViewOnlyTextCell] = cell
}
cell!.configCell("sometext", content: "sometext")
cell!.bounds = CGRectMake(0, 0, targetWidth, cell!.bounds.height)
cell!.contentView.bounds = cell!.bounds
cell!.setNeedsLayout()
cell!.layoutIfNeeded()
var size = cell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
size.width = targetWidth
return size
}else{
var cell = self.offscreenCells[CollectionViewKeyWords.collectioViewWithPhotoCell] as? CollectionViewCellWithPhoto
if cell == nil {
cell = NSBundle.mainBundle().loadNibNamed("CollectionViewCellWithPhoto", owner: self, options: nil)[0] as? CollectionViewCellWithPhoto
self.offscreenCells[CollectionViewKeyWords.collectioViewWithPhotoCell] = cell
}
cell!.configCell("Some text", content: "sometext")
cell!.bounds = CGRectMake(0, 0, targetWidth, cell!.bounds.height)
cell!.contentView.bounds = cell!.bounds
cell!.setNeedsLayout()
cell!.layoutIfNeeded()
var size = cell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
size.width = targetWidth
return size
}
}