mosaic-layout
mosaic-layout copied to clipboard
Headers not working?
Anyone else have trouble adding collectionview headers with the Mosaic Layout?
What kind of trouble?
Hey @vinnyoodles, thanks for the prompt reply. I'm not able to get the following delegate command to be called:
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
to create the header cell. Below is how I'm setting up the mosaic layout:
func setupCollectionView() {
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseHeaderIdentifier)
let mosaicLayout = TRMosaicLayout()
mosaicLayout.delegate = self
self.collectionView?.collectionViewLayout = mosaicLayout
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
fetchTestData()
}
Is there a stack trace for this error?
Nope, no error, header cell just never appears from a modified version of the TRCollectionViewController. Thought it was my implementation (probably still is), but I built a sample app with a basic collectionViewController and was able to get working headers to appear.
So my thought now is that I'm missing an extra setting or setup element to permit headers with the mosaic tiles?
What does your code look like for the header cell?
// Collection View Header method (never appears to be called)
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: reuseHeaderIdentifier, for: indexPath) as! LiveFeedHeaderCell
headerView.backgroundColor = UIColor.red
return headerView
}
// LiveFeedHeaderCell (stub at the moment)
import UIKit
class LiveFeedHeaderCell: UICollectionReusableView {
@IBOutlet weak var label: UILabel!
}
When you built the CollectionView did you use storyboard/xib or just code?
Edit: It looks like you are trying to register your header like you would register a cell, try to do something like this,
yourCollectionViewName.register(CellName.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: reuseHeaderIdentifier)
Thanks @Nathan961207. Using Storyboard. I updated the registration call to:
self.collectionView!.register(LiveFeedHeaderCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:reuseHeaderIdentifier)
But a header still does not show nor does the following delegate get called:
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
In Storyboard I've set the header cell background color to Blue, and in code I set it to Red, but neither are taking affect.
When building prototype cells (or headers) in the Storyboard, you should not register the cell for that class on the collectionView in code. This will overwrite your existing view and they won't appear.
http://stackoverflow.com/questions/33343973/swift-uicollectionview-custom-header-in-custom-layout-not-showing/33350781#33350781
Hi,
I also bumped to this kind of issue just recently. Header is not showing. I place a breakpoint at viewForSupplementaryElementOfKind delegate, however it doesn't get in to that area area of the code.
I tried commenting out this part of the code: // let mosaicLayout = TRMosaicLayout() // self.collectionView?.collectionViewLayout = mosaicLayout // mosaicLayout.delegate = self
Result: The header came out and also I pass to the area where breakpoint is set.
So when you are not using TRMosaicLayout the header works and when you use TRMosaicLayout it does not work? Could you provide more info about your code?
Hi,
Thanks for the reply.
I just did some modification on the sample project. Added a header to it.
TRCollectionViewController.swift - I added this code
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableView:UICollectionReusableView?
if kind == UICollectionElementKindSectionHeader {
let headerView:MosaicHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MosaicHeaderView", for: indexPath) as! MosaicHeaderView
reusableView = headerView
}
return reusableView!
}
MosaicHeaderView.swift - New file, no content in it though
import UIKit
class MosaicHeaderView: UICollectionReusableView {
}
Main.Storyboard - Added a header view to it, with a reuse identifier of "MosaicHeaderView"

Check this answer
http://stackoverflow.com/questions/33343973/swift-uicollectionview-custom-header-in-custom-layout-not-showing/33350781#33350781
On Wed, 22 Feb 2017 at 22:50, Jun [email protected] wrote:
Hi,
Thanks for the reply.
I just did some modification on the sample project. Added a header to it.
TRCollectionViewController.swift - I added this code
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableView:UICollectionReusableView? if kind == UICollectionElementKindSectionHeader { let headerView:MosaicHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MosaicHeaderView", for: indexPath) as! MosaicHeaderView reusableView = headerView } return reusableView!
}
MosaicHeaderView.swift - New file, no content in it though import UIKit class MosaicHeaderView: UICollectionReusableView { }
Main.Storyboard - Added a header view to it, with a reuse identifier of "MosaicHeaderView"
[image: screen shot 2017-02-23 at 5 47 05 am] https://cloud.githubusercontent.com/assets/5847140/23234037/c27cefae-f98b-11e6-8f37-8f824b249b7e.png
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/vinnyoodles/TRMosaicLayout/issues/9#issuecomment-281815741, or mute the thread https://github.com/notifications/unsubscribe-auth/AOmtnqz06ogF_cMJpxkG33P_MqgaXMi_ks5rfK2ygaJpZM4LF-vV .
Thank you.
Yes, I didn't register my story board header cell.
Has anyone solved this because I'm experiencing the same behavior. I can't get viewForSupplementaryElementOfKind to be called when I use TRMosaicLayout
I am having the same problem.
I'm having the same issue, was using FlowLayout before but after switching to TRMosaicLayout the header disappeared
did anyone find the fix, I am using customcollectionViewLayout and my header related functions are not executing and I don't know how to fix this