My UICollectionView's footer is not displaying any of its subviews. I've looked around for answers, but nothing seems to be working in my case. I'm not sure if it's something in Swift or I'm just missing something.
In my storyboard, I've checked the Section Footer
box and added a few buttons to the footer's view. I've set the reusable identifier to footer
and registered it in the view controller as a . Then I called collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!)
which sets it up. However, when I run the app, the footer does not show any subviews (footer.subviews.count
= 0). The footer's frame is correct, but why are the subviews I put in the storyboard not showing up?
Here's the code:
override func viewDidLoad() {
...
uiCollectionView.registerClass(MyFooterView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")
}
func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {
let reusableView:MyFooterView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "footer", forIndexPath: indexPath) as MyFooterView
println("Footer subivews: \(reusableView.subviews.count)") // 0
return reusableView
}
Try removing the registerClass line:
uiCollectionView.registerClass(MyFooterView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footer")
registerClass is only used if you are creating the view/cell programmatically. If you've created it in the storyboard, then registerClass will overwrite the one from the storyboard.
Using registerNib
instead of registerClass
worked for me,
IBcollectionView.registerNib(UINib(nibName: id, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: id)
registerClass
it just create new object from that class only, and doesn't load nib's view, but calling registerNib
does the trick.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With