UICollectionView AutoSize Header Height

I am encoding iOS 8+.

I have a UICollectionReusableView that is used as a UICollectionView header

 class UserHeader: UICollectionReusableView { ... } 

The My View collection does a few things:

Loads NIB into viewDidLoad

 override func viewDidLoad() { super.viewDidLoad() resultsCollectionView.registerNib(UINib(nibName: "UserHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader") } 

Sets the height of the header in referenceSizeForHeaderInSection .

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSizeMake(0, 500) } 

However, my UserHeader consisted of a lot of UILabel, UIViews that change the height at runtime, how can I specify the height for referenceSizeForHeaderInSection this dynamic? Or, if I should not use referenceSizeForHeaderInSection for autosizing in iOS 8+, please let me know what I should use. Thanks in advance.

For completeness, here's how I download the view, but I'm not sure if this is relevant for this discussion:

 func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! { var reusableview:UICollectionReusableView = UICollectionReusableView() if (kind == UICollectionElementKindSectionHeader) { let userHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader", forIndexPath: indexPath) as! UserHeader ... extra code to modify UserHeader reusableview = userHeaderView } return reusableview } 
+7
ios swift uicollectionview
source share
1 answer

I ran into a similar problem and fixed it using NSLayoutConstraint in the view to indicate the height of the header view. Inside the view controller, I then adjust the collection view header as follows:

 fileprivate var expectedSectionHeaderFrame = CGRect.zero let headerIdentifier = "HeaderView" func calculateHeaderFrame() -> CGRect { headerView.setNeedsLayout() headerView.layoutIfNeeded() let expectedHeaderSize = CGSize(width: view.bounds.width, height: headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height) return CGRect(origin: .zero, size: expectedHeaderSize) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { expectedSectionHeaderFrame = calculateHeaderFrame() return expectedSectionHeaderFrame.size } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { guard kind == UICollectionElementKindSectionHeader else { fatalError("Unexpected kind of supplementary view in (type(of: self))") } let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) headerView.frame = expectedSectionHeaderFrame return headerView } 
0
source share

All Articles