iamburak
iamburak

Reputation: 3568

How Can I resize collectionView header with referenceSizeForHeaderInSection?

I have a one section header and one UITextView in there, constraints 0 to all sides as Superview. Also I have a flow layout. I want to resize my header size after I filled my header reusable view using viewForSupplementaryElementOfKind.

How Can I reach my UITextView() in 0 section header. And than calculate my textView height to return exact value in this function.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    return CGSize(width: UIScreen.main.bounds.width, height: 200.0)
}

If someone explain this case, It would be great.

Upvotes: 2

Views: 593

Answers (1)

Megagator
Megagator

Reputation: 424

I was having a similar issue and finally tracked this down:

Make a UICollectionViewDelegateFlowLayout and then implement collectionView(_:layout:referenceSizeForHeaderInSection:)

So for example:

class ViewController: UIViewController,UICollectionViewDelegateFlowLayout {

...

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if section == 0 {
        return CGSize.zero
    }else{
        //...
    }

    return CGSize(width: collectionView.frame.width, height: 60)
}

Upvotes: 1

Related Questions