OUR BLOG

07 Dec 2020

UICollectionViewController 控制 cell 尺寸的大小 自动识别宽度

例子,需要extension 你的collectionviewcontroller的delegateFlowlayout里加入sizeForItemAt,实例为自动分成三列

//MARK: – UICollectionViewDelegateFlowLayout

extension ExploreCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (view.frame.width – 2) / 3
return CGSize(width: width, height: width)
}

admin