Search code examples
swiftuicollectionviewuiimageuiscrollviewdelegate

How to increase size of UICollectionView on Scroll


I want to increase the size of collectionView on Scroll. It's called Parallax affect.

I have 3 classes with named 1)class PhotoGalleryViewController' with storyboard. PhotoGalleryViewController contains collectionView and a cell with imageview. 2) class ListingDetailContentViewController' without storyboard 3) class ListingDetailViewController' with storyboard.

ListingDetailViewController is appending ListingDetailContentViewController as child.

contentViewController = ListingDetailContentViewController(hideComparable: comparableIsHidden)
        contentViewController.view.translatesAutoresizingMaskIntoConstraints = false
        contentViewController.listingDetailViewController = self
        addChild(contentViewController)
        view.addSubview(contentViewController.view)
        view.sendSubviewToBack(contentViewController.view)

ListingDetailContentViewController is appending photoGalleryViewController using Library AloeStackViewController. AloeStackViewController is UIScrollView so thats why i made its delegate in ListingDetailContentViewController

class ListingDetailContentViewController: AloeStackViewController, PhotoGalleryViewControllerDelegate, UIScrollViewDelegate {
// MARK: - Private functions

private func setupView(ad: Ad) {
    // Display photos
    if let photos = ad.photos {
        photoGalleryViewController = UIStoryboard(name: "Listing", bundle: nil).instantiateViewController(withIdentifier: "PhotoGalleryViewController") as? PhotoGalleryViewController
        let photoView = photoGalleryViewController.view!
        stackView.addRow(photoView)
        stackView.setInset(forRow: photoView, inset: .zero)
        photoGalleryViewController.photos = photos
        photoGalleryViewController.ad = ad
        photoGalleryViewController.delegate = self
        actualFrame = photoGalleryViewController.collectionView.frame.height
    }
// Some other stuff
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// The link i applied here is https://stackoverflow.com/questions/33481928/imageview-scaling-when-scrolling-down
        let offsetY = scrollView.contentOffset.y
        if offsetY < 0 {
            photoGalleryViewController.collectionView.frame.size.height +=  actualFrame - offsetY
            photoGalleryViewController.view.frame.size.height +=  actualFrame - offsetY
            self.loadViewIfNeeded()
        } else {
            photoGalleryViewController.collectionView.frame.size.height = actualFrame
            photoGalleryViewController.view.frame.size.height = actualFrame
            self.loadViewIfNeeded()
        }
    }
    }

I tried this help but not worked for me. My image is at top of screen. and other things are below. When ever user scrolls down. Image should be increase. Kindly help me out to solve this problem.

Update: Answer: I have done it with help of library called ParallaxHeader. I simply add this library using Pod and follow the tutorial given in library.


Solution

  • After a lot of struggle I achieved my goal that were asked in Question. I have done it with help of library called ParallaxHeader. I simply add this library using Pod and follow the tutorial given in library.