特定のセル数を表示するには、セルのサイズを計算する必要があります。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let numberOfRows = 2
let numberOfColumns = 3
let height = collectionView.frame.size.height/numberOfRows
let width = collectionView.frame.size.width/numberOfColumns
let cellSize = CGSize(width: width, height: height)
return cellSize
}
あなたのレイアウト構成が静的であるので、あなたはそれを速く作るために一度だけ、それを計算することができます:このような何か
class MyVC: UIViewController {
private let cellSize: CGSize = CGSize(width: collectionView.frame.width/numberOfColumns, height: collectionView.frame.height/numberOfRows) // or create a computed property
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return cellSize
}
}
このコードは、テストされていないが、多分あなたは、いくつかのパディングを追加する必要がありますが、あなたが得ますアイデア。もう1つのオプションは、自分自身を実装することですUICollectionViewLayout
サブクラス
私はここで良い解決策を見つけると思います:https://stackoverflow.com/questions/16678474/uicollectionview-horizontal-paging-can-i-use-flow-レイアウト – DonMag