2015-08-01 6 views
7

Kodowanie dla systemu iOS 8+.UICollectionView AutoSize wysokość nagłówka

Mam UICollectionReusableView który jest używany jako nagłówka UICollectionView

class UserHeader: UICollectionReusableView { 
... 
} 

moim zdaniem Kolekcja robi kilka rzeczy:

Ładunki NIB w viewDidLoad

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

Zestawy nagłówku wysokość w referenceSizeForHeaderInSection .

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

Jednak moja UserHeader widok składa się z wielu UILabel, UIViews kto wysokość zmienia się w czasie wykonywania, w jaki sposób można określić wysokość dla referenceSizeForHeaderInSection, który jest dynamiczny? Lub jeśli nie mam korzystać z automatycznej zmiany rozmiaru w iOS 8+ w wersji referenceSizeForHeaderInSection, proszę o informację, co powinienem użyć. Z góry dziękuję.

Przez wzgląd na kompletność, oto jak załadować widok, ale nie jestem pewien, czy to jest istotne dla tej dyskusji:

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 
} 

Odpowiedz

0

wychodził podobny problem i naprawić go za pomocą NSLayoutConstraint s w widoku aby określić wysokość widoku nagłówka. W ramach kontrolera widoku skonfiguruję następnie nagłówek widoku kolekcji tak:

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 
}