2016-01-27 8 views
9

मैं अपने शीर्षक और वास्तविक आइटम, जो वर्तमान में इस तरह दिखेगा के बीच कुछ अंतर जोड़ना चाहते हैं:UICollectionView: हेडर और आइटम के बीच अंतर को जोड़ने

screenshot1

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
    // Create header 
    switch kind{ 
     case UICollectionElementKindSectionHeader: 
      let headerView = iconCollectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "customIconHeaderView", forIndexPath: indexPath) as! CustonIconHeaderView 
      headerView.setUp() //add whatever into the view 
      return headerView 
     default: 
      assert(false, "Unexpected element kind") 
    } 

} 

उत्तर

0

एक तरह से आप कर सकते हैं का उपयोग कर अपने हैडर कंटेनर की अजी को बढ़ाने के लिए है

collectionView(_:layout:referenceSizeForHeaderInSection:)

उदाहरण:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width: 0, height: yourHeaderContentHeight + yourHeaderMarginToCell) 
} 

संपादित करें:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
    let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "YourID", forIndexPath: indexPath) 

    let yourCustomView = UIView(frame: CGRect(x: 0, y: 0, width: yourHeaderWidth, height: yourHeaderHeight)) 

    headerView.addSubview(yourCustomView) 

    return headerView 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width: yourHeaderWidth, height: yourHeaderHeight + yourHeaderMargin) 
} 
+0

जो कि हेडर की ऊंचाई में वृद्धि हुई है, मैं हेडर के लिए एक कस्टम दृश्य का उपयोग कर रहा हूं, हालांकि मुझे नहीं पता कि यह – Liumx31

+0

पर कुछ भी प्रभावित करता है उदाहरण के लिए, आपका कस्टम हेडर व्यू '40' pt ऊंचाई है और' संदर्भ आकारफोरहेडर इनसेक्शन' '60' है जो नीचे दिए गए '20' pt अंतर को प्रदान करेगा यदि आप अपना कस्टम हेडर व्यू' फ्रेम 'सही तरीके से सेट करते हैं :) – Breek

+0

मैं कस्टम व्यू की ऊंचाई कैसे सेट करूं, मैं कोड में सबकुछ कर रहा हूं। क्या मैं इसे 'viewForSplementmentElementOfKind' में करता हूं? – Liumx31

12

आप मूल रूप से आप अनुभाग के लिए शीर्ष इनसेट सेट होता है कि के लिए, संग्रह दृश्य अनुभाग के लिए एक शीर्ष मार्जिन जोड़ने के बारे में बात कर रहे हैं। कोड में यह करने के लिए, insetForSectionAtIndex लागू:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
    return UIEdgeInsets(top: 10.0, left: 1.0, bottom: 1.0, right: 1.0) 
} 

आप insetForSectionAtIndex लागू करने के लिए नहीं करना चाहते हैं, तो आप भी जैसे एक उचित विधि में कुछ इस तरह कर सकता है viewDidLoad: -:

enter image description here

नोट:

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout 
layout.sectionInset = UIEdgeInsets(top: 10.0, left: 1.0, bottom: 1.0, right: 1.0) 

इंटरफ़ेस बिल्डर, का चयन संग्रह ध्यान में रखते हुए और धारा Insets के लिए मान परिवर्तित> शीर्ष नीचे छवि में दिखाया के रूप में यह केवल यदि आप फ्लो लेआउट का उपयोग कर रहे हैं तो काम करता है।

+0

मैं कोड में सबकुछ कर रहा हूं , जब मैं 'insetForSectionAtIndex' बदलता हूं, तो यह शीर्षलेख के लिए शीर्ष मार्जिन भी बढ़ाता है। तो सब कुछ नीचे चला जाता है। – Liumx31

+0

क्या आप कह रहे हैं कि आप 'insetForSectionAtIndex' विधि को लागू नहीं करना चाहते हैं? यदि हां, तो आप यह भी कर सकते हैं: 'लेआउट = संग्रह.collectionViewLayout के रूप में दें! UICollectionViewFlowLayout; layout.sectionInset = UIEdgeInsets (शीर्ष: 10.0, बाएं: 1.0, नीचे: 1.0, दाएं: 1.0); ' – ishaq

+0

नहीं, मैंने' insetForSectionAtIndex' लागू किया, जो वर्तमान में 'UIEdgeInsets (शीर्ष: 0.0, बाएं: 20.0, नीचे है: 30.0, दाएं: 20.0) ', लेकिन जब मैं '10' कहने के लिए' शीर्ष 'बदलता हूं, न केवल आइटम नीचे जाते हैं, हेडर भी नीचे चला जाता है।लेकिन मैं जो चाहता हूं वह अनुभाग हेडर और आइटम्स के बीच बस अंतर है। – Liumx31

संबंधित मुद्दे