UICollectionView horizontal paging presenting 3 ce

2019-08-21 10:29发布

问题:

I have a UICollectionView that I have implemented to scroll horizontally but for some reason when I scroll through there are only 3 cells that are presented (when there should be 9)

class BusinessPhotosViewController: UICollectionViewController ,UICollectionViewDelegateFlowLayout{

    let cellId = "photos"

    override func viewDidLoad() {
        super.viewDidLoad()

        if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
            layout.minimumLineSpacing = 0
        }

        collectionView?.register(BusinessPhotoCells.self, forCellWithReuseIdentifier: cellId)
        collectionView?.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        collectionView?.scrollIndicatorInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        collectionView?.isPagingEnabled = true
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 9
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! BusinessPhotoCells

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = view.frame.width / 3
        return CGSize(width: width, height: width)
    }
}

I present this BusinessPhotosViewController within the cell of ANOTHER UICollectionViewController called BusinessDetailViewController

class BusinessDetailViewController : UICollectionViewController {

        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let scrollCell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureCellId, for: indexPath)

            let bizVC = BusinessPhotosViewController(collectionViewLayout: UICollectionViewFlowLayout())

            scrollCell.addSubview(bizVC.view)
            bizVC.view.anchor(top: scrollCell.topAnchor, left: scrollCell.leftAnchor, bottom: scrollCell.bottomAnchor, right: scrollCell.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: scrollCell.frame.width, height: scrollCell.frame.height)

            return scrollCell
    }
} 

So I can see that the scrolling works, but the issue I'm seeing is that only 3 cells are loaded (out of 9) and the view looks something like this:

I've seen these solutions here and here and other but none helped me.

回答1:

I tried your code and I can see all the 9 cells if the size of 9 cell is smaller than the size scroll cell size.

import UIKit

class ViewController: UIViewController{
    override func viewDidLoad() {
        view.backgroundColor = .red;

        view.addSubview(photosContainer);

        photosContainer.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true;
        photosContainer.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true;
        photosContainer.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true;
        photosContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4).isActive = true;

        ***let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .horizontal
        let bizVC = BusinessPhotosViewController(collectionViewLayout: layout)
        bizVC.view.frame = photosContainer.bounds;
        photosContainer.addSubview(bizVC.view)***

    }

    let photosContainer : UIView = {
        let view = UIView();
        view.backgroundColor =  .green;
        view.translatesAutoresizingMaskIntoConstraints  = false;
        return view;
    }();
}

Before adding the view if you set the frame then I can see all the nine cells no matter their size.

Let me know if it works