Search code examples
iosswiftuiviewcontrolleruicollectionviewcollectionview

UICollectionView inside UIViewController not allowing scroll


Edit1: I am not using a storyboard everything has been added programmatically
Edit2: Updated code snippet

Hello, i've got the following issues with UICollectionView

  • The CollectionView won't allow you to scroll no matter what you do.
  • Sometimes the cells even disappear complete leaving you with a white background CollectionView.

The problem occurs when you try to scroll up to view the cells that are not visible.

  • I've create a CollectionView which has different type of cells.
  • The CollectionView is nested inside a ViewController below an ImageView.
  • Constraints are added and they work just fine.

How do i make it scrollable?

GIF representing the problem

GIF Video

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
    self.navigationItem.title = "Featured"
    self.view.backgroundColor = UIColor.white
    
    // Add UImage carousel
    let carousel: UIImageView = {
        let imageView = UIImageView()
        let image = UIImage()
        
        imageView.backgroundColor = UIColor.purple
                    
        return imageView
    }()
    self.view.addSubview(carousel)
    carousel.translatesAutoresizingMaskIntoConstraints = false
    
    // Add CollectionView
    let featuredControllerLayout = UICollectionViewFlowLayout()
    // Add CollectionViewController
    featuredControllerLayout.scrollDirection = .vertical
    let featuredController = FeaturedCollectionViewController(collectionViewLayout: featuredControllerLayout)
    guard let featuredView = featuredController.collectionView else { return }
    
    self.view.addSubview(featuredView)
    featuredView.translatesAutoresizingMaskIntoConstraints = false
    
    // Setup Constraints
    if #available(iOS 11.0, *) {
        let guide = self.view.safeAreaLayoutGuide
        let guideSize = guide.layoutFrame.size
        
        carousel.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        carousel.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        carousel.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
        carousel.frame.size.height = guideSize.width/2
        carousel.heightAnchor.constraint(equalToConstant: carousel.frame.size.height).isActive = true
        
        featuredView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        featuredView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        featuredView.topAnchor.constraint(equalTo: carousel.bottomAnchor).isActive = true
        featuredView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
    }
}

Solution

  • Heyy what you are telling is you are not able to scroll up and see the entire collection view items ?

    if that is the case then you have not accounted the height of tabbar controller so you can enable translucent for example I have a git repo I made you can check and let me know if you are stuck anywhere

    https://github.com/dwivediashish00/socialApp