如何使用 UIView 子类作为 UICollectionView 页脚

How to use a UIView Subclass as a UICollectionView footer

我希望使用 UIView subclass 作为集合视图页脚 (UICollectionReusableView)。因为 class 已经写成 UIView

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    guard let footerView = self.collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier:"CustomUIView", forIndexPath: indexPath) as? UICollectionReusableView else { break }
    return footerView
}

如果您的 CustomUIView 不是 UICollectionReusableView 的子class,您将无法将其转换为 UICollectionReusableView

您有两个选择:

  • 如果它满足您的需要,您可以为您的 CustomUIView class subclass UICollectionReusableView,而不是 subclassing UIView

  • 由于 UICollectionReusableView 继承自 UIView,您可以创建一个 CustomUIView,然后创建一个新的 UICollectionReusableView 并添加 CustomUIView 你创建为子视图