Tobias Tobias - 4 months ago 20
iOS Question

UICollectionView 2 Different Background Colors

I try to avoid the white background color on top of my collectionview cell if the user scrolls down (basicly when it bounced). So on top of my cell is only the collectionview background color visible. But as shown in the picture I need 2 different colors. For the first section the background should be blue so the gap isn't white but blue and for the other sections it should remain white. So all I want is that the gap in white will become blue without changing the complete backgroundcolor of my uicollectionview.

enter image description here

Answer

You can achieve the desired effect by adding a view to the top your UICollectionView or UITableView - it will be scrolled along with the content and will appear as part of the background.

override func viewDidLoad() {
    super.viewDidLoad()
    let topView = UIView(frame: CGRect(x: 0, y: -collectionView!.bounds.height,
        width: collectionView!.bounds.width, height: collectionView!.bounds.height))
    topView.backgroundColor = .blackColor()
    collectionView!.addSubview(topView)
}
Comments