3254523
3254523

Reputation: 3026

How to use a UIView Subclass as a UICollectionView footer

I am looking to use a UIView subclass as a collection view footer (UICollectionReusableView). Since the class has already been written as a 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
}

Upvotes: 2

Views: 784

Answers (1)

JAB
JAB

Reputation: 3235

You won't be able to cast your CustomUIView as a UICollectionReusableView if it doesn't subclass from UICollectionReusableView.

You have a couple of options:

  • If it works for your needs, you can subclass UICollectionReusableView for your CustomUIView class, rather than subclassing UIView

  • Since UICollectionReusableView inherits from UIView, you could create a CustomUIView, then create a new UICollectionReusableView and add the CustomUIView you created as a subview

Upvotes: 4

Related Questions