Reputation: 62
In the lower swift I can use this function to set the size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
return CGSize(width: 300, height: 100)
}
but it is not work in swift 5.1
what function is fine in swift 5.1?
besides I have already inherited the UICollectionViewController
Upvotes: 0
Views: 1084
Reputation: 62
finally I find the method of referenceSizeForHeaderInSection
like @Raj Sharma and @Aira Samson said is depreted
I have specify the layout of the header and footer in the prepare()
function and return it to the layoutAttributesForItem
function and the layoutAttributesForElements
function.
the order is :
this is in swift 5.1.
Upvotes: 0
Reputation: 1147
Return Header or Footer height & width
extension YourCollectionViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 100)
}
}
Return UICollectionView Header or Footer
class YourCollectionViewController: UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if (kind == UICollectionView.elementKindSectionFooter) {
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath)
// Customize footerView here
return footerView
} else if (kind == UICollectionView.elementKindSectionHeader) {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath)
// Customize headerView here
return headerView
}
fatalError()
}
}
Upvotes: 0
Reputation: 243
You have to add UICollectionViewDelegateFlowLayout, hope this helps you :)
class SampleViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 300, height: 100)
}
}
Upvotes: 2