4

У меня есть UICollectionReusableView, который я хотел бы показать в заголовке моей коллекцииView.UICollectionReusableView height на основе текста UILabel

Я создал файл XIB для заголовка и перетащил UICollectionReusableView и выложил элементы внутри этого автомата. 2 ярлыков внутри многоразового представления имеют динамический контент, поступающий с сервера, и поэтому их высота меняется.

Я хотел бы, чтобы вычислить динамическую высоту во время выполнения и вернуть его из:

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

То, что я заметил, что высота зрения заголовка всегда равна высоте, которая установлена ​​в XIB файл.

enter image description here

+0

ли вы когда-нибудь решить эту проблему? –

ответ

0

для объективного и с внутри программно зрения действительно появился CGRect requiredHeight; // создаем в качестве переменной

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 
} 
Смежные вопросы