我正在尝试为 UICollectionView
创建一个简单的自定义布局,以便以编程方式使用(即不使用 Interface Builder)。
我按照文档做所有事情:
- 子类
UICollectionViewLayout
并准备我的布局 - 覆盖
layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath)
并返回相应的布局属性 - 添加
collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath)
实现,我将在其中返回补充 View
我的问题是两者都没有
collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath)
也不
layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath)
正在打电话,所以我看不到我的补充意见。
我已经仔细检查了布局和补充 View 创建的实现,但我仍然无法让它工作。
最佳答案
显然这还不够
layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
返回补充 View 的属性。还需要返回补充 View 的属性
layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
我的错误是我仅返回单元格的布局属性
layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
一旦我将补充 View 的布局属性添加到返回的值中
layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
我调用了数据源方法并返回了补充 View 。
编辑:此答案涵盖 iOS10 SDK。我没有尝试过早期版本
关于ios - UICollectionView 自定义布局。不显示或查询补充 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43046243/