首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子类UICollectionReusableView:引用“内容区域”

子类UICollectionReusableView:引用“内容区域”
EN

Stack Overflow用户
提问于 2015-07-10 16:55:33
回答 1查看 3.1K关注 0票数 0

我试图对UICollectionReusableView进行子类化,以简化一些代码。在构建视图之后,我遇到了一个“向其添加视图”的问题。

下面是我想要做的事情的一个例子,以UICollectionViewCell为例:

代码语言:javascript
复制
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”放在什么位置:

代码语言:javascript
复制
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”平行的地方。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-10 19:00:44

只需做self.addSubView(textLabel)

代码语言:javascript
复制
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
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31346316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档