有没有办法为UICollectionView的标题和内容视图设置不同的背景颜色?
我想要我的集合的标题背景颜色 - clearColor和内容背景为白色。
谢谢!
答案 0 :(得分:0)
您可以更改UICollectionViewCell
中的cellForItemAtIndexPath
背景颜色和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
}