[英]Center align single collectionview cell
如果只返回單個單元格,我想居中對齊UICollectionview單元格。 我正在用這個
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if self.imageSelected.count == 1
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 100, 0, 0)
}
}
return self.imageSelected.count
}
如果計數為1,則會選擇上面的代碼,但是在單元格位置沒有看到任何移動。
我也嘗試過這個,但是在這里甚至沒有達到斷點
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if self.imageSelected.count == 1 {
return UIEdgeInsetsMake(0, 100, 0, 0)
} else {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageSelected.count
}
將其放入您的視圖控制器中,而不是問題中的代碼:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if self.imageSelected.count == 1 {
return UIEdgeInsetsMake(0, 100, 0, 0)
} else {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
}
這使您創建的代碼起作用。 如果您需要有關如何將單元居中的更准確的答案,請詢問,我將花費一些時間和精力來幫助您:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.