Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

ios - Swift: How to mask a UICollectionView's background to only be seen behind visible cells?

Introduction

Hello,

I have added a background view with a CAGradientLayer, and my UICollectionViewCells have a hole in them to see the background. So far, it all looks great:

enter image description here


The Problem

However, when I scroll to the very top (and also the very bottom) of the UICollectionView, you can see the gradient background past the top of the first cell:

enter image description here


Question

What is something I can do to "mask" or "constrain" the gradient background to only being visible behind the cells, and not outside of the cell's frame/bounds?


Edit

This solves the top part, but I'm still not sure about the overflow on the bottom:

        let header = UIView()
        header.backgroundColor = UIColor.systemBackground
        collectionView.addSubview(header)
        
        header.snp.makeConstraints { (make) in
            make.top.equalTo(self.view)
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.collectionView.snp.top)
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...