gpt4 book ai didi

swift - 如何在 iOS TableViewCell 上实现圆角图像?

转载 作者:行者123 更新时间:2023-11-28 10:09:35 24 4
gpt4 key购买 nike

我正在尝试以这种方式将圆形图像附加到我的自定义表格单元格中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let content : Content = contentsCollection.contents[indexPath.row]
let channel : Channel = contentsCollection.channel(withId: content.channel!.id)

let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as! ContentCell
cell.layoutSubviews()

let channelPicture = URL(string: channel.picture)

cell.channelImageView.kf.setImage(with: channelPicture)

//Rounded image
cell.channelImageView.layer.cornerRadius = cell.channelImageView.frame.height/2

return cell
}

问题是,第一次显示 View Controller 时, ImageView 仍然是正方形。只有“隐藏”单元格具有圆形 ImageView 。

似乎是这篇文章中记录的问题:https://medium.com/@adinugroho/circular-uiimageview-in-uitableview-cell-e1a7e1b6fe63

但我无法为我的实现找到解决方法。

最佳答案

我认为缺少 clipsToBounds

参见 https://www.appcoda.com/ios-programming-circular-image-calayer/

我创建了一个 RoundImageView 类。

class RoundImageView: UIImageView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.cornerRadius = self.frame.height/2
self.clipsToBounds = true
}
}

在您的 Storyboard中实现这个。

enter image description here

对我有用。

备选方案:cell.channelImageView.clipsToBound = true 应该有效。

关于swift - 如何在 iOS TableViewCell 上实现圆角图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49699090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com