我想給一個UIImageView
圓形彩色背景UICollectionViewCell
。爲此,我正在將cornerRadius
應用於UIImageView
的layer
。UIImageView在圓形背景上,在UICollectionViewCell
顯然cornerRadius
需要是frame
的高度/寬度的一半。我把這個代碼放在layoutSubviews
方法中,這可能是錯誤的,因爲當它被稱爲圖像的frame
是巨大的。旋轉手機並旋轉回來解決了問題,因爲那時frame
已被正確設置。
我應該在哪裏放置cornerRadius
計算代碼?
無論如何,有沒有更好的方式在圓形背景上獲取圖像?
注意:UIImageView
可能會在佈局操作過程中更改高度/寬度。我正在使用Autolayout。
代碼:
class EventPickerCell: UICollectionViewCell
{
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var label: UILabel!
override func layoutSubviews()
{
super.layoutSubviews()
print("imagesizes = frame = \(image.frame), bounds = \(image.bounds)")
image.layer.cornerRadius = CGRectGetWidth(image.frame)/2
image.layer.backgroundColor = UIColor.whiteColor().CGColor
}
}
你試過'collectionView:willDisplayCell:'? –
謝謝@KyleRedfearn。我寧願將代碼保留在單元格內,但不要求集合視圖涉及單元格的外觀。這就是爲什麼我最初覆蓋集合視圖單元格的原因。 – HughHughTeotl
我想你將不得不使用委託和數據源。這就是他們爲 –