首先,在 viewcontroller 上先加个UICollectionViewDelegateFlowLayout
如下:
class TemplateCollectionViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {
}
实现下面两个layout 的 delegate 方法即可
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 20
let collectionViewSize = collectionView.frame.size.width - 3*padding
return CGSize(width: collectionViewSize/2, height: **)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 20, 0, 20)
}
网友评论