我试图对UICollectionReusableView进行子类化,以简化一些代码。在构建视图之后,我遇到了一个“向其添加视图”的问题。
下面是我想要做的事情的一个例子,以UICollectionViewCell为例:
class CVCell: UICollectionViewCell {
let textLabel: UILabel!
override init(frame: CGRect) {
let textFrame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
textLabel = UILabel(frame: textFrame)
super.init(frame: frame)
textLabel.font = UIFont.systemFontOfSize(10)
textLabel.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
textLabel.textAlignment = NSTextAlignment.Center
textLabel.backgroundColor = UIColor.clearColor()
contentView.addSubview(textLabel)
}
required init(coder aDecoder: NSCoder) {
...
}
}在这里,当我准备添加UILabel时,我将它添加到contentView中。我似乎找不到与UICollectionReusableView并行的子视图来参考。因此,我写了这篇文章,并进入最后一行,我不知道应该把占位符“ConfusionHere”放在什么位置:
class CVHeaderView: UICollectionReusableView {
let textLabel: UILabel!
override init(frame: CGRect) {
let textSize = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
textLabel = UILabel(frame: textSize)
super.init(frame: frame)
textLabel.font = UIFont.systemFontOfSize(10)
textLabel.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
textLabel.textAlignment = NSTextAlignment.Center
textLabel.backgroundColor = UIColor.clearColor()
ConfusionHere.addSubview(textLabel)
}我可能在这里遗漏了一些概念,但我已经搜索了文档,没有发现与UICollectionReusableView的“UICollectionReusableView”平行的地方。
发布于 2015-07-10 19:00:44
只需做self.addSubView(textLabel)
class CVHeaderView: UICollectionReusableView {
let textLabel: UILabel!
override init(frame: CGRect) {
let textSize = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
textLabel = UILabel(frame: textSize)
super.init(frame: frame)
textLabel.font = UIFont.systemFontOfSize(10)
textLabel.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
textLabel.textAlignment = NSTextAlignment.Center
textLabel.backgroundColor = UIColor.clearColor()
self.addSubview(textLabel) // confusion removed
}https://stackoverflow.com/questions/31346316
复制相似问题