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")
}
}