2
votes

I'm trying to create a collection view that can be resized manually.

The cells inside the collectionView have the width of the screen and the height of the collection view.

I have the entire thing working, except the cells don't update constraints when a user changes the collection view height. They only update if a new cell appears

I've tried different ways to make this height animate directly but no solution seems to be changing the cell height with the collection view height instantly.

Is there a work-around to make the cell sizes animate in size?

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
              
    let width = self.CollectionViewController2.frame.width as CGFloat
    let height = self.CollectionViewController2.frame.height as CGFloat     
        
    return CGSizeMake(self.CollectionViewController2.frame.width, self.CollectionViewController2.frame.height)                           
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell: CustomCell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell", forIndexPath: indexPath) as CustomCell

            let height = self.CollectionViewController2.frame.height

            cell.frame = CGRectMake(cellB.frame.origin.x, 0, cellB.frame.size.width, height)
            cell.updateConstraintsIfNeeded()
            cell.layoutIfNeeded()
            cell.imgs.image = UIImage(named: somePictures[indexPath.row])

            return cell


}

A Button that allows a Swipe Gesture to resize the collection view:

    @IBAction func SwipeDownStatus(sender: AnyObject) {

        if CollectionViewHeight.constant == 390 {
            self.CollectionViewHeight.constant = 130
            self.CommentLikeBarHeight.constant = -50
            CollectionViewController2.updateConstraints()
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.view.layoutIfNeeded()
            })
        }
1

1 Answers

0
votes

Firstly, the way you change the row height is not correct. It's better to outlet the height constraint of the item which will be expanded (in this case I guess is the UIImageView) and change it when you want to expand. it makes Autolayout to take control over the layout correctly. To animate it, the only thing you should do is this:

heightConstant.constant = 50 // your desired height
UIView.animate(withDuration: 0.25, animations: {
        self.view.layoutIfNeeded()
    }, completion: nil)