簡體   English   中英

如何在Collectionview單元格中為Uiimageview設置圓角

[英]How to set rounded corner for Uiimageview in Collectionview cell

我正在嘗試根據以下設計制作UICollectionviewCell

在此輸入圖像描述

我已經為單元格設置了角半徑,但是當在collectonview上加載數據時, imageview沒有得到圓角。 我得到的輸出如下

在此輸入圖像描述

圓角應用於單元格,但不應用於圖像。 這是我正在申請的代碼。 代碼應用於collectionView_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

cell.layoutIfNeeded()

cell.layer.cornerRadius      = 15

cell.imageView.clipsToBounds = true

cell.layer.masksToBounds     = true

有人可以幫我解決這個問題。

提前致謝....

你需要設置imageView layer corner半徑: -

cell.imageView.layer.cornerRadius = 15
cell.imageView.layer.masksToBounds = true

這樣做:

cell.contentView.layer.cornerRadius = 2.0
cell.contentView.layer.borderWidth = 1.0
cell.contentView.layer.borderColor = UIColor.clear.cgColor
cell.contentView.layer.masksToBounds = true;

這是如果你想為背景添加一個漂亮的陰影:

cell.layer.shadowColor = UIColor.lightGray.cgColor
cell.layer.shadowOffset = CGSize(width:0,height: 2.0)
cell.layer.shadowRadius = 2.0
cell.layer.shadowOpacity = 1.0
cell.layer.masksToBounds = false;
cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath

您還可以使用“用戶定義的運行時屬性”將角半徑設置為您的imageview Keypath - layer.cornerRadius類型 - 數字值 - 您的角半徑值

你可以說: cell.clipsToBounds = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM