【发布时间】:2017-02-09 12:06:00
【问题描述】:
我有一个简单的问题,在谷歌、文档或此处找不到解决方案。
我的视图控制器中有一个 Collectionview。我创建了一个自定义单元,DescriptionCell,其中包含一个 UIImage。我希望这张图片有圆角。但是,我不知道在哪里设置 UIImage 层上的cornerradius。我在单元格的 awakeFromNib 方法、委托方法 CellForRowAtIndexPath 中尝试过,并在单元格中覆盖了 LayoutSubview 但它不起作用。 设置 UIImage 半径的代码应该放在哪里?
要指定,我知道如何创建 UIImage 的圆角。但是如果是Collectionview单元格的子视图,不知道在哪里设置cornerradius。
这是我的 descriptionCell 的代码
class DescriptionCell: UICollectionViewCell {
@IBOutlet weak var mImage: UIImageView!
override func awakeFromNib() {
//mImage.layer.cornerradius = 5
//Does not work, the image is still square in the cell
}
在单元格中为RowAtIndePath
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("descriptioncell", forIndexPath: indexPath) as! DescriptionCell
//cell.mImage.layer.cornerradius = 5
//Does not work, the image is still square in the cell
return cell
}
提前致谢!
【问题讨论】:
-
不,这是不同的。我已经在使用该答案中的代码。我的问题是 UIImage.layer.cornerradius = xxx 放在哪里?
-
用相关代码更新您的问题。
-
没有可以添加的特定代码。我的问题很简单:如何在 UICollectionViewCell 中创建一个带有圆角的 UIImage。
-
@Jell92 至少为您的
DescriptionCell和CellForRowAtIndexPath添加代码是有意义的。
标签: ios swift uiimage viewcontroller collectionview