2016-07-25 58 views
1

我有一个名为ChronicleCellUICollectionViewCell自定义类。将UIButton设置为UICollectionViewCell的大小

我正在创建一个类型为UIButton的实例变量。

class ChronicleCell : UICollectionViewCell { 
    //init etc. 
    var buttonFullCellSize : UIButton = { 
     let button = UIButton() 
     //TODO set button to be size of UICollectionViewCell frame 

    }() 

} 

对于此实例变量,我想设置其帧等同于UICollectionViewCell的帧。但是,从UICollectionViewCell继承的类中无法访问。我怎样才能做到这一点?谢谢!

+2

你看过访问'self.contentView'获取单元格的所有大小信息? –

回答

2

细胞的contentView是尺寸与电池,所以你应该利用这个来获得帧相同。

var buttonFullCellSize: UIButton { 
    let button = UIButton() 
    button.frame = contentView.frame 
    return button 
} 
+0

@Allanah_Douglas当我写我的ChronicleCell类内我得到以下错误'实例成员'contentView'不能用于类型'ChronicleCell'' – Thalatta

+0

啊,但我认为这是因为我试图访问闭包内的类属性! – Thalatta

2

分配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 
    }() 

}