2017-08-16 84 views
0

我有粘頭UICollectionView隱藏/阻止細胞滾動背後粘頭

集合視圖
flowLayout.sectionHeadersPinToVisibleBounds = true 

我的問題是,我的頭的上半部分是半透明的,當我滾動我的手機了,我可以看到單元格在標題後面滾動。

我想隱藏標題後面的部分單元格視圖。在附上的圖片中,我不想看到紅色背後的綠色。剩下的行爲我想保持原樣。

我之所以需要這個是我在很後面,我需要顯示

Sticky CollectionViewHeaders

@IBOutlet weak var collectionView: UICollectionView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    self.collectionView.alwaysBounceVertical = true 
    collectionView.register(UINib.init(nibName: EXAMPLE_CELL_REUSE_ID, bundle: nil), forCellWithReuseIdentifier: EXAMPLE_CELL_REUSE_ID) 
    collectionView.register(UINib.init(nibName: EXAMPLE_HEADER_REUSE_ID, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: EXAMPLE_HEADER_REUSE_ID) 

    if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { 
     flowLayout.headerReferenceSize = CGSize(width: 400, height: 100) 
     flowLayout.sectionHeadersPinToVisibleBounds = true 
    } 
} 




func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return sections.count; 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 1 //self.sections[section].1; 
} 



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let exampleCell = collectionView.dequeueReusableCell(withReuseIdentifier: EXAMPLE_CELL_REUSE_ID, for: indexPath) as! MyCellCollectionViewCell; 

    exampleCell.headerLabel.text = "Cell" 
    exampleCell.backgroundColor = UIColor.green 

    return exampleCell; 
} 


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    if kind == UICollectionElementKindSectionHeader { 
     if let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: EXAMPLE_HEADER_REUSE_ID, for: indexPath) as? ExampleHeader { 
     // header.backgroundColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 0.5) 
     return header 
     } else { 
     return UICollectionReusableView() 

     } 
    } 
    return UICollectionReusableView() 
} 

我覺得這裏的問題可能是類似的壁紙圖像,但沒有迴應並不清楚它是否是同一個問題。 Transparent sticky header ui collectionview don't show cells underneath

+0

顯示您已使用的代碼。 – PGDev

+0

除了sectionHeadersPinToVisibleBounds之外,代碼是標準的。我反正張貼 –

回答

0

我已經創建了一個非常簡單的設置,像你的,它工作正常。

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

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     self.collectionView.alwaysBounceVertical = true 

     if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout 
     { 
      flowLayout.headerReferenceSize = CGSize(width: 400, height: 100) 
      flowLayout.sectionHeadersPinToVisibleBounds = true 
     } 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int 
    { 
     return 10 
    } 

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

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

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView 
    { 
     if kind == UICollectionElementKindSectionHeader 
     { 
      return collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "rview", for: indexPath) 
     } 
     return UICollectionReusableView() 
    } 
} 
+0

什麼工作正常?我的收藏視圖起作用,我的問題是如何擁有一個透明的標題,而不會看到它背後的單元格。 –

+0

你是如何使標題透明的?我的意思是這樣做的代碼 – PGDev

+0

哦..真的很抱歉..我完全不瞭解你的問題。此鏈接https://stackoverflow.com/questions/33010199/make-transparent-stickyheader-like-weather-ios可能會幫助你。 – PGDev