0

I'm having issues with the dynamic height of the collection view's header on tab change. I have a collection view of multiple sections. In one of the header sections, I'm using a segment view with two tabs and under each tab, there is a table view. The expectation is the collection view header height should be adjusted so as to show the full content of the table view without any scroll for each tab which contains different data.

On segment change action, I've tried reloading that particular section of the collection view but it's not taking up the full height as per the content inside the table view and it's showing scroll.

`@objc func availabilitySegmentChangeAction() { DispatchQueue.main.async {

   self.collectionView.reloadSections([3])
   self.collectionView.collectionViewLayout.invalidateLayout()

} }


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




`

func setAvailabilityInfoPresentedView(_ collectionView: UICollectionView, referenceSizeForHeaderInSection section: Int) -> CGSize{
        let indexPath = IndexPath(row: 0, section: section)
        if let headerView = self.collectionView(collectionView, viewForSupplementaryElementOfKind: UICollectionView.elementKindSectionHeader, at: indexPath) as? AddOrEditCollectionReusableView {
            // Use this view to calculate the optimal size based on the collection view's width
            let size = headerView.systemLayoutSizeFitting(CGSize(width: collectionView.frame.width, height: UIView.layoutFittingExpandedSize.height),
                                                          withHorizontalFittingPriority: .required, // Width is fixed
                                                          verticalFittingPriority: .fittingSizeLevel)
            return CGSize(width: size.width, height: size.height)
        }
        return .zero
    }


//header view class, table view content size observer

`override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "contentSize" {
            if let newvalue = change?[.newKey] {
                let newsize = newvalue as? CGSize
                self.tableViewHeightConstraint.constant = (newsize?.height ?? 0)
                print("header view table view height ", newsize?.height)
            }
        }
    }`



Malika Arora
  • 439
  • 1
  • 4
  • 6

0 Answers0