[英]Set a UIButton to the size of a UICollectionViewCell
我有一個名為ChronicleCell
UICollectionViewCell
自定義類。
我正在創建類型UIButton
的實例變量。
class ChronicleCell : UICollectionViewCell {
//init etc.
var buttonFullCellSize : UIButton = {
let button = UIButton()
//TODO set button to be size of UICollectionViewCell frame
}()
}
對於此實例變量,我想將其框架設置為與UICollectionViewCell
的框架等效。 但是,這不能從UICollectionViewCell
繼承的類中UICollectionViewCell
。 我怎樣才能做到這一點? 謝謝!
將collectionViewLayout的框架分配給按鈕的框架:
let rect = self.collectionViewLayout.layoutAttributesForItemAtIndexPath(clIndexPath).frame
button.frame = button.rect
您的代碼可能是:
class ChronicleCell : UICollectionViewCell {
//init etc.
var buttonFullCellSize : UIButton = {
let button = UIButton()
let frameOfUICollectionViewCell = self.collectionViewLayout.layoutAttributesForItemAtIndexPath(clIndexPath).frame
button.frame = button.rect
return button
}()
}
單元格的contentView
與單元格的大小相同,因此您應該使用它來獲取框架。
var buttonFullCellSize: UIButton {
let button = UIButton()
button.frame = contentView.frame
return button
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.