如何通过垂直滚动一次显示collectionView单元格一次

时间:2019-07-10 07:12:20

标签: swift layout uicollectionviewcell

我想在集合视图中一次显示一个单元格,同时垂直滑动。但它显示的是下一个视图的一半。如何一次为一个单元设置它?

expanded

t

3 个答案:

答案 0 :(得分:1)

您需要从UICollectionViewDelegateFlowLayout继承,然后使用

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

答案 1 :(得分:1)

UICollectionViewFlowLayout具有委托方法sizeForItemAt,该方法可用于为您显示的CGSize赋予UICollectionViewCell。您只需要保持一致就可以了!

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
     //....

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.size //In case if your collection is of full size
     }
}

答案 2 :(得分:0)

在创建UICollectionView时,添加FlowLayout

    let layout = UICollectionViewFlowLayout.init()
    layout.scrollDirection = UICollectionView.ScrollDirection.vertical
    layout.itemSize = CGSize(width: collectView_WIDTH, height: collectView_HEIGHT)
    let collectionViewItem = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
    collectionViewItem.setCollectionViewLayout(layout, animated: true)

添加

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: collectView_WIDTH, height: collectView_HEIGHT)

}