我试图在 UIViewController 的每一行后面显示一个装饰 View 。
每个“行”有 3 个 UICollectionViewCells。我想通过三个单元格行后面的装饰 View 显示背景。
在 viewDidLoad: 我正在注册装饰 View :
[self.collectionView.collectionViewLayout registerClass:[CollectionRowDecorationView class] forDecorationViewOfKind:@"RowDecorationView"];
我还设置了以下委托(delegate)方法:
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
layoutAttributes.frame = CGRectMake(0.0, 0.0, self.collectionView.contentSize.width, self.collectionView.contentSize.height);
layoutAttributes.zIndex = -1;
return layoutAttributes;
}
不幸的是,委托(delegate)方法没有被调用。通过阅读文档,我不是 100% 清楚我需要做什么才能初始化装饰 View ?看起来我缺少另一个委托(delegate)方法,需要调用该方法才能正确设置所有内容。
谢谢!
最佳答案
您需要在 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 中添加装饰 View ,如下所示:
[attributesArray addObject:[self layoutAttributesForDecorationViewOfKind:@"BackgroundView" atIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
关于ios - UICollectionViewFlowLayout UICollectionReusableView DecorationView 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13148998/