Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Footer view for UICollectionview in swift

I am re-writting an objective-C app of min in swift and decided that a collection view works better then a tableview in my case. So i have the collectionview all set up exactly how i want it and now i need to add a footer to my collection view.

In objective-C it was very easy for me, all i did was create a UIView, add objects to it, and add it to my tableview like this.

self.videoTable.tableFooterView = footerView;

Now i've been trying in my collection view to get a similar solution, however i've had no luck so far.

Is there a simple .collectionviewFooterView or something that i can add my UIView to?

EDIT i found similar ideas HERE if that helps create an answer

EDIT

I've been thinking on ways to achieve this so right now my idea is to add the footer view to the end of the collection view using the following code:

var collectionHeight = self.collectionView.collectionViewLayout.collectionViewContentSize().height

footerView.frame = CGRectMake(0, collectionHeight, screenSize.width, 76)

The only issue i'm having with this workaround is i need to add more space to the contentView of the colletionView and i am having no luck with that

My Solution

I solved it using a work-around (not the prettiest but it works), i add the UIView to the uicollectionview using a simple

footerView.frame = CGRectMake(0, collectionHeight - 50, screenSize.width, 50)
self.collectionView.addSubview(footerView)

and set the layout insets using this:

alLayout.contentInsets = UIEdgeInsetsMake(2, 2, 52, 2)
like image 398
inVINCEable Avatar asked Nov 12 '14 16:11

inVINCEable


People also ask

How do you add header and footer view in UICollectionView?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .

How does swift implement UICollectionView?

Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices. The foundation is now set up in the storyboard.


1 Answers

Collection views handle headers and footers differently than table views. You'll need to:

  1. Register your footer view class using:

    registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
    
  2. Either set the headerReferenceSize on your collection view layout or implement collectionView:layout:referenceSizeForHeaderInSection: in your delegate.

  3. Return the footer view from the following method in your dataSource:

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView", forIndexPath: indexPath)
        // configure footer view
        return view
    }
    

I think that's everything!

like image 146
Nate Cook Avatar answered Oct 14 '22 12:10

Nate Cook