答案 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)
}