簡體   English   中英

將UIButton設置為UICollectionViewCell的大小

[英]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.

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