'Move UICollectionView to the top of screen while user is scrolling up and stick it

First of all i have to say i am new to designs like the one i am describing. I am trying to copy the functionality of the Instagram app when you tap on the + button to select a new image on the screen. So i am trying to do a simple example.

I have a UIViewController that consists of a UICollectionView and on the top of this UICollectionView another UIView exists. The functionality i need to achieve is the following. When the user scrolls up i need the UIView to move up until it is out of the screen. Then the UICollectionView should stick to the top of the screen and the user should be able to scroll down the UICollectionView and select an image. If he scrolls up and reaches top of the UICollectionView and then if he scrolls down the UIView should move down as well along with the UICollectionView. Also if he selects an image at some point when the UIView is out of screen it should move back down to the original position and show the image.

In ViewDidScroll and scrollViewWillBeginDragging i use the following code

 (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.selectedContentPreviewContainerView.translatesAutoresizingMaskIntoConstraints = YES;
    self.selectedContentPreviewContainerView.translatesAutoresizingMaskIntoConstraints = YES;
}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect rect1 = self.selectedContentPreviewContainerView.frame;
    rect1.origin.y = -scrollView.contentOffset.y;
    self.selectedContentPreviewContainerView.frame=rect1;
}

The UIView is going up if i scroll up on the UICollectionView and the UICollectionView as well moves up but the height of the UICollectionView remains the same and also i do not know how to stick the UICollectionView to the top of screen when the UIView is out of screen. Please anyone guide me on how to achieve the functionality. Any help appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source