0
votes

I'm creating an app as Instagram. So, I have an UICollectionView, where I placed my images, user details and likes&comments.

So, the problem is, that I set cells size in

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

and set the initial size as (width: 320, height: 420(320 — image, 50 — user details on top of the image and 50 — likes&comments under an image)).

But depends on likes and comments count, my bottom view size may change, for example from 50 to 200. But in this case my UICollectionViewCell does not change its size.

How can I make that depends on comments view height my UICollectionViewCell height will change too?

I'm working over it over one week and do not know how to implement it. Can anyone please help me?

1
Have you set vertical constraints correctly?zcui93
you need to implement a method to calculate the cell's size based on its content, and use this method in the sizeForItemAtIndexPath methodMudOnTire
@zcui93 yes, I supposeJohn Doe
@MudOnTire I have custom cell, so it's in another file(customCell.swift)John Doe

1 Answers

0
votes
you need to get the height of string and then set cell size aacording to that string!!! 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
height=_collectionView.contentSize.height;
    width = _collectionView.frame.size.width;
    _collectionView.frame = CGRectMake(0,20,width, height);
}

#pragma MARK COLECTIONVIEW DELEGATE FLOW LAYOUT
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize Size =[[self.Array_LIKEs objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
    CGFloat calculate_width =Size.width+Size.width/2+20;
    CGFloat calculate_height=Size.height+10;
    return  CGSizeMake(calculate_width, calculate_height);

}