UICollectionView 2 Different Background Colors

2019-04-22 05:10发布

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

1条回答
倾城 Initia
2楼-- · 2019-04-22 05:49

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)
}
查看更多
登录 后发表回答