Spacing between uiimage and label inside uicollectionviewcell. Dynamic height of cell

345 views Asked by At

I'm trying to get my label and image to dynamically change the height of a uicollectionviewcell. How can I estimate the correct height of what the cell should be from each image obtained? It is expecting a width and height, but I don't want to distort the image.

I'm following this cocoapod: https://github.com/ecerney/CollectionViewWaterfallLayout

Or would there be a better approach to setting the label/text?

Currently the spacing between the label and image is not set...

enter image description here

The cell inside the storyboard looks like the following with UIImageView set to Aspect Fill:

enter image description here

Code to obtain an image set

lazy var imageSet: [UIImage] = {
    var _imageSet = [UIImage]()
    for x in 0...10{
        let array = [600,800,900]
        let array2 = [1000,1200,1400]
        let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
        let randomIndex2 = Int(arc4random_uniform(UInt32(array2.count)))

        let urlString:String = String(format: "http://lorempixel.com/%@/%@/", String(array[randomIndex]),String(array2[randomIndex2]))
        let image = UIImage(data: NSData(contentsOfURL: NSURL(string: urlString)!)!)
        print(urlString)
        print("\(x)\n")
        _imageSet.append(image!)
    }
    return _imageSet
}()

Code for setting the size of each cell...

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


    let thisLayout = layout as! CollectionViewWaterfallLayout


    var globalImage = imageSet[indexPath.row]

    var finalWith = globalImage.size.width
    var finalHeight = globalImage.size.height

    var newSize = CGSize(width: finalWith, height: finalHeight )

    return newSize
}

My repo https://github.com/rlam3/waterfallCocaopod

0

There are 0 answers