snapchatdotcom snapchatdotcom - 2 months ago 19x
Swift Question

Scrolling collection view with UIViews on top?

I have a UICollectionViewController and I added two UIViews as subviews. One is the purple UIView and above it is another white UIView, with the blue collection view getting scrolled up behind both.

Below those UIViews, I made the

from the top 300 (that's the total size of two UIViews' height). What I'm trying to accomplish is to scroll the collection view along with the two UIViews above. It's almost similar to the solution on this thread (Move a view when scrolling in UITableView), except when I override scrollViewDidScroll, the whole frame gets scrolled up and cells go behind the two Views. All I want is to scroll up the UIViews, and then scroll through the collection views. I feel like this might involve nested scroll views.

enter image description here

This was how I overrode scrollViewDidScroll:

var rect = self.view.frame
rect.origin.y = -scrollView.contentOffset.y - 300
self.view.frame = rect

EDIT: I posted a video that demonstrates what I want to do per iOS Tumblr app:


I have achieved the same requirement through some basic steps as below.

//Declare the view which is going to be added as header view
    let requiredView = UIView()

//Add your required view as subview of uicollectionview backgroundView view like as
collectionView.backgroundView = UIView() 

//After that control the frame of requiredHeaderView in scrollViewDidScroll delegate method like
        func scrollViewDidScroll(scrollView: UIScrollView) {
         let per:CGFloat = 60 //percentage of required view to move on while moving collection view
                let deductValue = CGFloat(per / 100 * requiredView.frame.size.height)
                let offset = (-(per/100)) * (scrollView.contentOffset.y)
                let value = offset - deductValue
                let rect = requiredView.frame
                self.requiredView.frame = CGRectMake(rect.origin.x, value, rect.size.width, rect.size.height)