0

I am trying to adjust the height of my CollectionView Header based on the height of myView's height. for example I want to be able to type:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 100)
} //Instead of 100 I want to say: **myView.frame.height**

but since I have created myView inside the header class I can't access it, I was wondering if anyone could help me with this or has any better way of doing this. I would really appreciate it!

This is my code:

class MyProfileCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView?.register(MyProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
    }



    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind:
        String, at indexPath: IndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
            headerIdentifier, for: indexPath) as! MyProfileHeader
        header.backgroundColor = .red
        return header

    } //HERE
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: view.frame.width, height: 111)
    }

}

And this is my Header class where I have created myView:

  class MyProfileHeader: UICollectionReusableView, UICollectionViewDelegate {

        override init(frame: CGRect)    {
            super.init(frame: frame)
            self.addSubview(myView)

            myView.widthAnchor.constraint(equalToConstant: 50).isActive = true
            myView.heightAnchor.constraint(equalToConstant: 70).isActive = true
            myView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            myView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        }

//I want my headers height be equal to myView's height so if i change myView's height the headers height changes automatically.
        var myView: UIView = {
            let view = UIView()
            view.translatesAutoresizingMaskIntoConstraints = false
            view.backgroundColor = .gray
            return view
        }()

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
William
  • 3
  • 3
  • You can always locate your views if you have access to your collection view. [supplementaryView(forElementKind:at:)](https://developer.apple.com/documentation/uikit/uicollectionview/1618041-supplementaryview) – Desdenova Oct 25 '18 at 06:22

1 Answers1

0

Somehow you are using the magic number for the height of your myview inside MyProfileHeader class like myView.heightAnchor is 70. For a quick solution, you can use a static variable inside MyProfileHeader or outside.

    class MyProfileHeader: UICollectionReusableView, UICollectionViewDelegate {

        static var height: CGFloat = 70.0

        override init(frame: CGRect)    {
            super.init(frame: frame)
            self.addSubview(myView)

            myView.widthAnchor.constraint(equalToConstant: 50).isActive = true
            myView.heightAnchor.constraint(equalToConstant: MyProfileHeader.height).isActive = true
            myView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            myView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        }
- - - - - -
}

Now you can use that in layout return:

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

        return CGSize(width: view.frame.width, height: MyProfileHeader.height)

    }
Razib Mollick
  • 4,617
  • 2
  • 22
  • 20