如何在swift 4.2中增加UICollectionView
Cell宽度和高度等于屏幕?
您需要更改单元格大小以适合collectionview
。在viewDidLoad
中添加此内容
if let flow = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flow.itemSize = self.collectionView.frame.size
}
如果你的UICollectionViewCell
有一个UIImageView
的子视图,它被锚定以完全填充它(即UIImageView以顶部,底部,左边和右边的方式锚定UICollectionViewCell)。
可以使用以下方法根据屏幕屏幕大小调整UICollectionViewCell
的大小,这将自动导致UIImageView
与屏幕本身一样大。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return self.collectionView.bounds.size //to ensure that this works as intended set self.collectionView size be the screen size.
}
请注意,UICollectionView
足够大,可以容纳你所寻求的UICollectionViewCell
的大小。