Increasing `collectionViewContentSize` width causes CollectionView Cells to disappear

106 Views Asked by At

So I have an issue I've been battling with for days now. I have a collectionView which is designed to have large cells (width-wise) which go off screen. The height is fixed. The idea is to basically scroll to the end (see below):

enter image description here

To achieve being able to scroll left and right, I've had to override the width inside collectionViewContentSize in my flowLayout. Like below.

    override var collectionViewContentSize: CGSize {
        var size = super.collectionViewContentSize
        size.width = largeWidth
        return size
    }

This achieves increasing the horizontal scroll area (which is what I want) but the cells start to disappear once I reach a certain point. It's almost as if the cells are being dequeued when they shouldn't be. Any ideas on this. This is the final straw for my project but I'm out of ideas.

Many thanks

Code snippet can be found below. You should be able to just copy and paste this into any project:

class HomeViewController: UIViewController {
    
    let collectionView: UICollectionView
    let collectionViewLayout = CustomCollectionViewFlowLayout()
    
    init() {
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
        super.init(nibName: nil, bundle: nil)
        
        collectionView.backgroundColor = UIColor.red.withAlphaComponent(0.4)
        collectionView.register(SomeCell.self, forCellWithReuseIdentifier: "SomeCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.frame = view.bounds
        view.addSubview(collectionView)
    }
}

class SomeCell: UICollectionViewCell {
    
}

extension HomeViewController: UICollectionViewDataSource,
                              UICollectionViewDelegate,
                              UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 150
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SomeCell", for: indexPath) as! SomeCell
        cell.backgroundColor = .blue
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 10000, height: 70)
    }
}


class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
    override var collectionViewContentSize: CGSize {
        var size = super.collectionViewContentSize
        size.width = 10000
        return size
    }
}
0

There are 0 best solutions below