Objective c 动态更改UICollectionReuseableView(集合节标题)的高度
我试图动态设置Objective c 动态更改UICollectionReuseableView(集合节标题)的高度,objective-c,ios6,xcode4.5,Objective C,Ios6,Xcode4.5,我试图动态设置UICollectionView的节标题高度,但是使用下面的代码,我看不到任何变化。绘制视图时,视图中包含正确的项目,但高度不会移动。如果这是一个重复的问题,很抱歉,但我似乎找不到任何与UICollectionView对象相关的内容。提前谢谢 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfK
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;
}
试着这样做:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *header = [siteMapCollection dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerID" forIndexPath: indexPath];
NSString *headerText = @"This is my header";
UIFont *labFont = [UIFont fontWithName: @"HelveticaNeue-CondensedBold" size: 20.0];
CGSize textSize = [dummyText sizeWithFont: labFont];
UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, header.frame.size.height - (textSize.height + 12), header.frame.size.width, textSize.height + 8)];
[headerLabel setFont: labFont];
[header addSubview: headerLabel];
return header;
}
假设您使用的是流程布局,则您的代理应实现以下功能:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
您可以为每个标题返回不同的大小。在水平滚动的集合视图中,仅使用宽度。在垂直滚动中,仅使用高度。未使用的值将被忽略-视图将始终拉伸以分别填充水平/垂直集合视图的全高/全宽
页脚也有相应的方法。Swift 3和4中接受的答案:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 250)
}
好的,与使用嘈杂的委托方法不同,我更喜欢使用以下方法:(仅当您具有唯一的头大小时才有效) 也适用于itemSize:
layout.itemSize = CGSize(width: 42, height: 42) //Set a custom item size
例如,这对于设置相对于屏幕宽度的项目大小非常有效。这是有效的,但如何判断我正在编辑哪个部分,我只想使用方法中的
section
参数对索引1处的部分执行此操作。在ivar中存储您正在编辑的部分,然后进行比较。如果它们不相同,请返回版面的headerReferenceSize
。很抱歉,这里是一个完整的noob,但headerReferenceSize不是collectionViewLayout的一部分。。。我是否需要创建标题实例来读取帧?否-强制转换collectionViewLayout
-返回[(UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout headerReferenceSize]代码>.Hero@ashfourrow-这正是我想要的,完美:)谢谢!
layout.itemSize = CGSize(width: 42, height: 42) //Set a custom item size