【问题标题】:Round imageview in UICollectionViewCell after setting cell size with UICollectionViewDelegateFlowLayout使用 UICollectionViewDelegateFlowLayout 设置单元格大小后,在 UICollectionViewCell 中圆形图像视图
【发布时间】: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


    【解决方案1】:

    试试这个

    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
    
    
            cell.imgMain.clipsToBounds = true
            cell.imgMain.contentMode = .scaleAspectFill
        }
        return cell
    }
    

    Cell.Swift中添加这段代码

    class CSCollectionViewCell : UICollectionViewCell {
        
        @IBOutlet weak var imgMain: UIImageView!
        @IBOutlet var lbl: UILabel!
        
        
        override func awakeFromNib() {
               super.awakeFromNib()
               // Initialization code
           }
           
        override func draw(_ rect: CGRect) {
                super.draw(rect)
                self.layer.cornerRadius = self.frame.size.width / 2
            }
    
        
    }
    
                
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多