我有水平的CollectionView,当我启动应用程序时,我的画廊中的最后一张图片只显示了一半。如何解决?
答案 0 :(得分:3)
我也遇到过类似的问题,而对我来说,帮助我设置MinimumInteritemSpacingForSectionAt和minimumLineSpacingForSectionAt
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
答案 1 :(得分:2)
contentView框架可能比CollectionView框架大。
假设您希望collectionView显示5个单元格......
let cvWidth = collectionView.frame.width
let cvHeight = collectionView.frame.height
let cellEdge = cvWidth / 5
let contentSize = cellEdge * CGFloat(numberOfCells)
collectionView.contentSize = CGSize(width: contentSize, height: cvHeight)
不要忘记设置 numberOfCells 值
如果单元格之间有一些空格,则应以这种方式设置cellEdge:
let cellEdge = cvWidth / 5 + space
要设置单元格和节之间的空间,首先将UICollectionViewFlowLayoutDelegate添加到ViewController:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
比你可以通过这个函数设置间隙,行间距和插图:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
答案 2 :(得分:1)