4

I have a UICollectionReusableView which I would like to show in the header of my collectionView.

I created a XIB file for the header and and dragged a UICollectionReusableView and laid out the elements inside that using auto layout. 2 of the labels inside the reusable view have dynamic content coming from a server and so their height varies.

I would like to calculate the dynamic height at runtime and return it from :

collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize

What I have noticed is that the height of the header view is always equal to the height that is set in the XIB file.

enter image description here

Mohit Athwani
  • 835
  • 9
  • 17

1 Answers1

0

for objective c and programatically inside view did appear CGRect requiredHeight;//create as variable

labelToUseInHeader = [self createLblWithfontStyle:@"thin" fontSize:16 frameDimen:CGRectMake(0, 0,collectionView.frame.size.width-16, 40) andTextColor:UIColorFromRGB(0x000000)];

CGSize constrainedSize = CGSizeMake(labelToUseInHeader.frame.size.width,9999);

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:                                       [UIFont fontWithName:@"HelveticaNeue-Thin" size:16.0], NSFontAttributeName,nil];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:"stringToProcessFromServer" attributes:attributesDictionary];

requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
requiredHeight = CGRectMake(0,0,labelToUseInHeader.frame.size.width, requiredHeight.size.height);

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {

        reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        if (reusableview==nil)
        {
            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width,200)];
        }

        labelToUseInHeader=[self createLblWithfontStyle:@"thin" fontSize:16 frameDimen:CGRectMake(8,0, collectionView.frame.size.width-16, 40) andTextColor:UIColorFromRGB(0x000000)];
        labelToUseInHeader.numberOfLines=0;
        labelToUseInHeader.userInteractionEnabled=NO;
        labelToUseInHeader.text="string from server";
        [labelToUseInHeader sizeToFit];

        [reusableview addSubview:labelToUseInHeader];
    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ 
return CGSizeMake(collectionView.frame.size.width,requiredHeight.size.height+10);//+10 for padding
 }
Mohit Athwani
  • 835
  • 9
  • 17
Tanuj Jagoori
  • 199
  • 2
  • 9