Deepak Kumar
Deepak Kumar

Reputation: 59

Change UICollectionView Height Dynamically according to cell's not change cell height

I am developing IOS App.Click to Tableview cell Than value show on collectionViewCell but I want change collectionView height according to cell's.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(_collectionView.frame.size.width , _collectionView.frame.size.height+30);

}

Upvotes: 1

Views: 1829

Answers (2)

Er. Khatri
Er. Khatri

Reputation: 1414

you can change it by using contentSize of its scrollView after the collectionview is loaded.. if you update any cell insert items or delete items you need to update height again i.e.

myCollectionView.reload()

now updating frame or height

//if you are using auto-resizing you can update frame
let frame = myCollectionView.frame
frame.height = myCollectionView.contentSize.height
myCollectionView.frame = frame

and if you are using autolayout make and iboutlet for your collectionview's height constraint

@IBOutlet collectionViewHeightConstraint:NSLayoutContraint!

now update this contraint

collectionViewHeightConstraint.constant = myCollectionView.contentSize.height

Upvotes: 2

Nupur Gupta
Nupur Gupta

Reputation: 285

You need to add this code

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    var numberOfCellInRow : Int = yourArray.count
    var padding : Int = 5
    var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding)
    return CGSize(width: collectionCellWidth , height: collectionCellWidth)
}

Upvotes: 0

Related Questions