【问题标题】:iOS Swift - UICollectionView - Sections different background coloriOS Swift - UICollectionView - 不同背景颜色的部分
【发布时间】:2016-02-22 15:35:04
【问题描述】:

有没有办法为 UICollectionView 的标题和内容视图设置不同的背景颜色?

我希望我的收藏的标题背景颜色 - clearColor 和内容背景为白色。

谢谢!

【问题讨论】:

  • UICollectionViewUIScrollView 的子类。 UIScrollView 实际上操纵了它的子视图的边界,所以集合视图的背景实际上并没有移动。您可以检查contentOffset 并检测您何时进入下一部分。
  • 看看这个。一个很好解释的答案:stackoverflow.com/questions/18317405/…

标签: ios swift background header uicollectionview


【解决方案1】:

您可以更改cellForItemAtIndexPath 中的UICollectionViewCell 背景颜色和viewForSupplementaryElementOfKind 中的部分标题背景

override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.backgroundColor = UIColor.magentaColor()
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.whiteColor()
            return cell
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return   2
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 10
    }

    override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath)
        if kind == UICollectionElementKindSectionHeader {
            headerView.backgroundColor = UIColor.clearColor()
        }
        return headerView
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2014-06-29
    • 2014-09-13
    • 1970-01-01
    相关资源
    最近更新 更多