4

我有一个UICollectionReusableView,我想在我的collectionView的标题中显示。基于UILabel文字的UICollectionReusableView高度

我为这个头文件创建了一个XIB文件,并且拖拽了一个UICollectionReusableView,并在其中使用自动布局来布局这些元素。可重复使用视图中的2个标签具有来自服务器的动态内容,因此它们的高度各不相同。

我想在运行时计算动态高度和返回它:

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

我已经注意到的是,头视线的高度总是等于在厦门国际银行设定的高度文件。

enter image description here

+0

你有没有解决这个问题? – 2017-02-24 18:47:03

回答

0

客观c和编程 内景确实出现 的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 
}