尝试确定子类 UICollectionViewCell 中的单元格宽度

Trying to determine cell width in a subclassed UICollectionViewCell

我有一个基本的子类 UICollectionView Cell。我想向其中添加一个 UIImageView,以便每个单元格都显示一个图像。

如果我向 x:y:width:height 添加明确的值,图像会正确显示,但我无法使用 self.contentView.frame.width 来确定集合视图单元格的大小(用于将图像放置在 x轴)

class SubclassedCell: UICollectionViewCell {
    var myImageView: UIImageView!
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        myImageView = UIImageView(frame: CGRect(x: (self.frame.width - 10.0), y: 50.0, width: 20.0, height: 20.0))
        myImageView.image = UIImage(named: "JustinBieber")
        self.contentView.addSubview(myImageView)
    }
}

在上面,(self.contentView.frame.width - 10.0) 没有获取集合视图单元格的大小,所以图像根本不显示。如果我明确地为 x 输入一个值,比如说 0,它就会显示出来。

如何确定子类集合视图单元格的大小(宽度)?

在视图生命周期的早期调用初始化程序以准确提供维度值。

更惯用的方法是在 layoutSubviews 生命周期方法中布局图像。一个简单的例子如下所示

class SubclassedCell: UICollectionViewCell {

    var myImageView: UIImageView!
    var imageDisplayed = false

    override func layoutSubviews() {
        super.layoutSubviews()
        if !imageDisplayed {
            myImageView = UIImageView(frame: CGRect(x: (self.frame.width - 10.0), y: 50.0, width: 20.0, height: 20.0))
            myImageView.image = UIImage(named: "JustinBieber")
            self.contentView.addSubview(myImageView)
            imageDisplayed = true
        }
    }
}

如果您在应用程序中使用自动布局,您可能还需要考虑添加图像并提供约束,而不是显式设置图像的框架。

如此 answer 中所示 - 根据您的用例 - 它们可能是设置您的手机的更好方法。

有很多方法可以实现这种布局,但有一些限制,您不需要知道宽度。您定义所需的关系,其余的由布局系统管理。

class SubclassedCell1: UICollectionViewCell {
    var myImageView: UIImageView!

    private func commonInit() {
        myImageView = UIImageView()
        myImageView.image = UIImage(named: "JustinBieber")
        myImageView.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(myImageView)

        NSLayoutConstraint.activate([
            myImageView.widthAnchor.constraint(equalToConstant: 20),
            myImageView.heightAnchor.constraint(equalToConstant: 20),
            myImageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 50),
            myImageView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 10),
            ])
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        commonInit()
    }
}

您也可以使用 layoutSubviews,如@syllabix 所述。执行此操作时,请确保在每次调用 layoutSubviews 时更新框架,否则如果调整大小,您的视图将无法正确更新。在布置其子项时,也使用父视图边界,而不是框架。

class SubclassedCell2: UICollectionViewCell {
    var myImageView: UIImageView!

    private func commonInit() {
        myImageView = UIImageView()
        myImageView.image = UIImage(named: "JustinBieber")
        self.contentView.addSubview(myImageView)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        commonInit()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        myImageView.frame = CGRect(x: (self.bounds.maxX - 10.0), y: 50.0, width: 20.0, height: 20.0)
    }
}