ios - UICollectionView 自定义布局。不显示或查询补充 View

标签 ios uicollectionview uikit uicollectionviewlayout

我正在尝试为 UICollectionView 创建一个简单的自定义布局,以便以编程方式使用(即不使用 Interface Builder)。

我按照文档做所有事情:

  1. 子类UICollectionViewLayout并准备我的布局
  2. 覆盖 layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) 并返回相应的布局属性
  3. 添加 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/

相关文章:

ios - 如何在 iOS 中以编程方式绘制到显示屏?

iOS 8 Swift UICollectionView reloadData() 导致 View (图像)跳单元格

ios - 如何使用 IOS 12 在 UITableViewCell 中正确添加 UICollectionView

ios - 基于内容的动态 UITableView 单元格高度

ios - WillTerminate 事件永远不会被调用

ios - 单击tableview Cell中的Collectionview时如何找到tableview的索引路径

iphone - UIButton 文本边距/填充

ios - 我应该为 LaunchScreen.storyboard 中的图像提供什么尺寸的 @3x、@2x 等?

ios - 与 iOS 分发系统混淆

iOS:如何从此 for 循环中获取每个字符串并将其添加到 NSMutableArray 中?