ios - UICollectionView 水平分页仅在第一页上每行显示 3 个单元格

标签 ios swift uiscrollview uicollectionview uicollectionviewcell

我有一个 UICollectionView,我已经实现了水平滚动,但由于某种原因,当我滚动时,只显示了 3 个单元格(本应有 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)
    }
}

我在另一个名为 BusinessDetailViewController 的 UICollectionViewController 的单元格中呈现此 BusinessPhotosViewController

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
    }
} 

所以我可以看到滚动有效,但我看到的问题是只加载了 3 个单元格(总共 9 个),并且 View 看起来像这样:

enter image description here

我见过这些解决方案herehere和其他但没有帮助我。

最佳答案

我尝试了你的代码,如果 9 个单元格的大小小于滚动单元格的大小,我可以看到所有 9 个单元格。

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;
    }();
}

在添加 View 之前,如果您设置了框架,那么我可以看到所有九个单元格,无论它们的大小。

Screenshot

让我知道它是否有效

关于ios - UICollectionView 水平分页仅在第一页上每行显示 3 个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633427/

相关文章:

ios - 在sqlite中是否必须使用“END TRANSACTION”

iphone - init中使用loadNibNamed时,ViewController收不到viewDidLoad事件

ios - 自定义 UICollectionViewCell

ios - 如何在不同设备的段控制中管理文本大小?

ios - MKMapView 的内存使用率很高

ios - 在 WatchOS 中以模态呈现的界面的右上角设置标题

ios - SpriteKit 防止 Sprite 在没有弹跳的情况下离开屏幕

ios - matchedGeometryEffect 动画效果不流畅

ios - UIScrollView 内的多个 UITextField 在一定数量后不可单击

iphone - UIScrollView 缩放后无法平移