Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

ios - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...