2017-08-16 49 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