我正在尝试为待办事项列表类型的应用程序制作详细信息屏幕.这是详细屏幕目前的样子:
这是一个带有标题的UICollectionViewController.标头包含2个UILabel对象和一个UITextView对象.这些对象的布局由垂直UIStackView管理. UIView用于设置白色背景.
我在运行时定义此UICollectionReusableView的高度时遇到了一些困难.任何建议表示赞赏.
最佳答案 这有点像黑客,但似乎有效.
// showhere to keep a reference
UICollectionReusableView * _cachedHeaderView;
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if(!_cachedHeaderView){
// dequeue the cell from storyboard
_cachedHeaderView = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"header_cell"] forIndexPath:indexPath];
// set captions/images on the header etc...
// tell the collectionview to redraw this section
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
}
return _cachedHeaderView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
// once there is a reference ot the view, use it to figure out the height
if(_cachedHeaderView){
return [_cachedHeaderView systemLayoutSizeFittingSize:collectionView.bounds.size withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityDefaultLow];
}
// a placeholder value just to get the dequeueReusableCellWithReuseIdentifier to work
return CGSizeMake(collectionView.bounds.size.width, 100);
}