gpt4 book ai didi

ios - 修改 Collection View 单元格内容的图层属性

转载 作者:行者123 更新时间:2023-11-30 14:11:11 25 4
gpt4 key购买 nike

在修改 UIImageView 的 layer 属性时,我得到了混合结果。在 UICollectionView 中,我有 UICollectionViewCells,其中有一个 UIImageView。我希望这些图像看起来像圆圈,因此,在我的 UICollectionViewCell 子类中,我重写 layoutSubviews 并将代码放在那里,如下所示:

override func layoutSubviews() {
super.layoutSubviews()
image.layer.cornerRadius = CGRectGetWidth(image.bounds) / 2
image.layer.masksToBounds = true
}

但是,我得到的结果不可靠。有时,图像是完美的圆形(如预期),但有时它们是带有圆角边缘的正方形,如果您滚动 collectionView,它们可能会也可能不会变成圆形。

放置此代码的最佳位置在哪里?

最佳答案

这取决于图像的大小。您需要有方形图像才能使用cornerRadius 创建圆形。您可以通过设置图像的宽度约束,然后将纵横比设置为 1:1 来实现此目的。

关于ios - 修改 Collection View 单元格内容的图层属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31754189/

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