【发布时间】:2021-12-18 11:45:06
【问题描述】:
我在 UICollectionView 单元格中使用图像视图。我在 UICollectionViewDelegateFlowLayout 的帮助下设置单元格的大小。我希望图像正确圆形,但它形成椭圆。从其他视图控制器移回后,它变成圆形。我的代码是
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let yourWidth = collectionView.bounds.width/2.0 - 10.0
let yourHeight = yourWidth
return CGSize(width: yourWidth, height: yourHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return totalCard.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColCell", for: indexPath) as! ColCell
cell.imgMain.image = UIImage(named: "temp.png")
if (self.totalCard.count-1) == indexPath.item {
cell.lblTitle.text = "Add link"
cell.imgMain.image = UIImage(named: "addCard.png")
}else{
cell.lblTitle.text = mediaTmp.cardName
let url = URL(string: mediaTmp.cardImage)
cell.imgMain.kf.setImage(with: url)
cell.imgMain.layer.cornerRadius = cell.imgMain.frame.height/2
}
return cell
}
我尝试重新加载collectionview数据但没有用。
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell