Dir*_*alb 29 objective-c ios6 xcode4.5
我试图UICollectionView
动态设置节标题的高度,但使用下面的代码,我没有看到任何变化.视图是使用正确的项目绘制的,但高度不会让步.很抱歉,如果这是一个重复的问题,但我似乎找不到任何与该UICollectionView
对象有关的内容.提前致谢.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
PhotoVideoHeaderCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"videoHeaderView"
forIndexPath:indexPath];
if (indexPath.section == 0) {
// photos
[headerView setSection:@"Photo"];
} else {
[headerView.VehicleDetailView removeFromSuperview];
CGRect frame = headerView.frame;
frame.size.height = 60;
[headerView setFrame:frame];
[headerView setNeedsDisplay];
[headerView setBackgroundColor:[UIColor grayColor]];
[headerView setSection:@"Video"];
}
return headerView;
}
Run Code Online (Sandbox Code Playgroud)
Ash*_*row 72
假设您正在使用流布局,您的委托应该实现以下函数:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
Run Code Online (Sandbox Code Playgroud)
您可以为每个标题返回不同的大小.在水平滚动集合视图中,仅使用宽度.在垂直滚动的中,仅使用高度.未使用的值将被忽略 - 您的视图将始终被拉伸以分别填充水平/垂直集合视图的整个高度/宽度.
页脚也有相应的方法.
Swift 3 和 4 中接受的答案:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 250)
}
Run Code Online (Sandbox Code Playgroud)
好吧,我不喜欢使用嘈杂的委托方法,而是更喜欢使用这个:(仅当您有唯一的标头大小时才有效)
let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout // Assuming you use the default flow layout
layout.headerReferenceSize = CGSize(width: 42, height: 42) //Set the header size
Run Code Online (Sandbox Code Playgroud)
也适用于 itemSize:
layout.itemSize = CGSize(width: 42, height: 42) //Set a custom item size
Run Code Online (Sandbox Code Playgroud)
例如,这对于设置相对于屏幕宽度的项目大小非常有效。