2015-08-01 9 views
7

मैं आईओएस 8+ के लिए कोडिंग कर रहा हूं। viewDidLoadUICollectionView ऑटोसाइज हेडर ऊंचाई

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

सेट में

भार निब referenceSizeForHeaderInSection में ऊंचाई शीर्षक:

मैं एक UICollectionReusableView कि UICollectionView

class UserHeader: UICollectionReusableView { 
... 
} 

मेरे देखें संग्रह हेडर के रूप में इस्तेमाल किया जा रहा करता है कुछ चीजें है ।

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

लेकिन, मेरा UserHeader दृश्य कई UILabel, UIViews के शामिल है जो ऊंचाई रन टाइम पर बदल जाता है, मैं कैसे referenceSizeForHeaderInSection कि गतिशील है के लिए एक ऊंचाई निर्दिष्ट कर सकते हैं क्या है? या यदि मुझे आईओएस 8+ में ऑटो-साइजिंग के लिए referenceSizeForHeaderInSection का उपयोग नहीं करना है, तो कृपया मुझे बताएं कि मुझे क्या उपयोग करना चाहिए। अग्रिम में धन्यवाद।

पूर्णता के लिए, यहाँ मैं कैसे दृश्य लोड है, लेकिन मुझे यकीन है कि अगर यह चर्चा के लिए प्रासंगिक है नहीं कर रहा हूँ:

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 
} 

उत्तर

0

मैं ने वही समस्या का सामना करना पड़ा और दृश्य के भीतर NSLayoutConstraint रों का उपयोग करके इसे ठीक किया गया हेडर व्यू की ऊंचाई निर्दिष्ट करने के लिए। दृश्य नियंत्रक के भीतर मैं संग्रह संग्रह के शीर्षलेख को इस प्रकार कॉन्फ़िगर करता हूं:

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 
} 
संबंधित मुद्दे