UICollectionView 自定义布局。不显示或查询补充视图

use*_*176 3 uikit ios uicollectionview uicollectionviewlayout

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

我根据文档做所有事情:

  1. 子类UICollectionViewLayout并准备我的布局
  2. 覆盖layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath)并返回相应的布局属性
  3. collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath)在我返回补充视图的地方添加实现

我的问题是既不

collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)

也不

layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath)
Run Code Online (Sandbox Code Playgroud)

正在被调用,所以我看不到我的补充意见。

我已经仔细检查了我的实现布局和补充视图的创建,但我仍然无法让它工作。

use*_*176 6

显然这还不够

layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
Run Code Online (Sandbox Code Playgroud)

它返回补充视图的属性。还需要返回补充视图的属性

layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
Run Code Online (Sandbox Code Playgroud)

我的错误是,我是唯一回来的布局属性的细胞

layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
Run Code Online (Sandbox Code Playgroud)

一旦我将补充视图的布局属性添加到由

layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
Run Code Online (Sandbox Code Playgroud)

我调用了我的数据源方法并返回了补充视图。

编辑:此答案涵盖 iOS10 SDK。我没有试过更早的版本