2016-10-03 9 views
5

-मेकअप UIcollectionView हैडर गतिशील ऊंचाई

मैं हैडर
साथ UIcollectionView है हैडर प्रकार UICollectionReusableView
है और इसकी एक पाठ लेबल और इस पाठ का आकार सुलझाई है, इसके क्या उपयोगकर्ता द्वारा दर्ज
पर निर्भर करता है मैं तो मेरी headr जरूरत खींच रहा हैडर
में जोड़ देगा लेबल और अन्य वस्तुओं की ऊंचाई पर निर्भर करता है यह मेरा स्टोरीबोर्ड है

enter image description here

इस उत्पादन जब मैं अनुप्रयोग चलाने

enter image description here

+0

शायद यह काम है, लेकिन मैं उद्देश्य-सी को समझ नहीं पा रहा हूं, कृपया मुझे इसे – joda

+0

http://stackoverflow.com/questions/37873537/how-to- गतिशील-add-labels-and-buttons-in में चाहिए -section-header-of-uicollectionview – joda

+0

शायद, प्रश्न डुप्लिकेट किया गया है: http: // stackoverflow।कॉम/प्रश्न/256424 9 3/डायनामिक-यूकोलेक्शनव्यू-हेडर-साइज-आधारित-ऑन-यूलाबेल –

उत्तर

3

नोट: इस स्विफ्ट 3 कोड है।

आपका ViewController:

class ViewController: UIViewController { 
    @IBOutlet weak var collectionView: UICollectionView! 

    // the text that you want to add it to the headerView's label: 
    fileprivate let myText = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda." 
} 

extension ViewController: UICollectionViewDataSource { 
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     switch kind { 
     case UICollectionElementKindSectionHeader: 
      let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, 
                      withReuseIdentifier: "customHeader", 
                      for: indexPath) as! CustomCollectionReusableView 

      headerView.lblTitle.text = myText 
      headerView.backgroundColor = UIColor.lightGray 

      return headerView 
     default: 
      fatalError("this is NOT should happen!!") 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 100 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) 

     cell.backgroundColor = UIColor.brown 
     // ... 

     return cell 
    } 
} 

extension ViewController: UICollectionViewDelegateFlowLayout { 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
     // ofSize should be the same size of the headerView's label size: 
     return CGSize(width: collectionView.frame.size.width, height: myText.heightWithConstrainedWidth(font: UIFont.systemFont(ofSize: 17))) 
    } 
} 


extension String { 
    func heightWithConstrainedWidth(font: UIFont) -> CGFloat { 
     let constraintRect = CGSize(width: UIScreen.main.bounds.width, height: CGFloat.greatestFiniteMagnitude) 
     let boundingBox = self.boundingRect(with: constraintRect, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) 

     return boundingBox.height 
    } 
} 

आपका कस्टम UICollectionReusableView:

class CustomCollectionReusableView: UICollectionReusableView { 
    @IBOutlet weak var lblTitle: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     // setup "lblTitle": 
     lblTitle.numberOfLines = 0 
     lblTitle.lineBreakMode = .byWordWrapping 
     lblTitle.sizeToFit() 
    } 
} 

आशा इस मदद की।

5

यह मैं बिल्कुल के बारे में आधे से एक दिन के लिए पागल चलाई। आखिर में क्या काम करता है।

सुनिश्चित करें कि आपके शीर्षक में लेबल गतिशील आकार होने के लिए, एक पंक्ति और लपेटकर

  1. एक दृश्य में अपने लेबल एम्बेड सेट हैं। यह autosizing के साथ मदद मिलेगी। enter image description here

  2. सुनिश्चित करें कि आपके लेबल पर बाधाएं सीमित हैं। पूर्व: नीचे लेबल से पुन: प्रयोज्य दृश्य तक बाधा से अधिक काम नहीं करेगा। उपरोक्त छवि देखें।

  3. एक आउटलेट अपने उपवर्ग के दृश्य आपको

    class CustomHeader: UICollectionReusableView { 
        @IBOutlet weak var contentView: UIView! 
    } 
    
  4. में अपने लेबल एम्बेडेड के लिए प्रारंभिक लेआउट

    override func viewWillLayoutSubviews() { 
        super.viewWillLayoutSubviews() 
    
        collectionView.collectionViewLayout.invalidateLayout() 
    } 
    
  5. लेआउट हैडर सही आकार

    पाने में जोड़े अमान्य
    extension YourViewController: UICollectionViewDelegate { 
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    
         if let headerView = collectionView.visibleSupplementaryViews(ofKind: UICollectionElementKindSectionHeader).first as? CustomHeader { 
          // Layout to get the right dimensions 
          headerView.layoutIfNeeded() 
    
          // Automagically get the right height 
          let height = headerView.contentView.systemLayoutSizeFitting(UILayoutFittingExpandedSize).height 
    
          // return the correct size 
          return CGSize(width: collectionView.frame.width, height: height) 
         } 
    
         // You need this because this delegate method will run at least 
         // once before the header is available for sizing. 
         // Returning zero will stop the delegate from trying to get a supplementary view 
         return CGSize(width: 1, height: 1) 
        } 
    
    } 
    
+0

यह समझने की ज़रूरत नहीं है कि यह लंबे सूचियों के साथ काम नहीं करेगा क्योंकि उस सूची में लौटने पर हेडर दिखाई नहीं देगा। एक छोटी सूची के लिए यह अभी भी काम करेगा। –

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