How to get the frame size of a UIImageView in a UICollectionView cell

狂风中的少年 提交于 2019-12-11 17:42:59

问题


I'm trying to get the frame size of a UIImageView inside a UICollectionViewCell. I have currently tried to achieve this using thumbnailImageView.frame.size but this returns (0.0, 0.0) no matter where I call this method from.

The reason I want to do this is to round the edges of the image inside the image view, but as I am using .scaleAspectFit I need to know the frame size and image size to correctly round the corners.

Below is the cell class:

class SomeCollectionViewCell: UICollectionViewCell {

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

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let thumbnailImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    private func setLabel(label: UILabel, textSize: CGFloat = 15.0, textColour: UIColor = UIColor.black) -> UILabel {
        label.textColor = textColour
        label.textAlignment = .center
        label.font = label.font.withSize(textSize)
        return label
    }

    var firstLabel: UILabel = UILabel()
    var secondLabel: UILabel = UILabel()
    var thirdLabel: UILabel = UILabel()

    func setupViews() {
        firstLabel = setLabel(label: firstLabel)
        secondLabel = setLabel(label: secondLabel)
        thirdLabel = setLabel(label: thirdLabel)

        addSubview(thumbnailImageView)
        addSubview(firstLabel)
        addSubview(secondLabel)
        addSubview(thirdLabel)

        addConstraintWithFormat(format: "V:|-25-[v0(125)]-[v1(16)]-[v2(16)]-[v3(17)]-25-|", views: thumbnailImageView, firstLabel, secondLabel, thirdLabel)
        addConstraintWithFormat(format: "H:|[v0]|", views: thumbnailImageView)
        addConstraintWithFormat(format: "H:|[v0]|", views: firstLabel)
        addConstraintWithFormat(format: "H:|[v0]|", views: secondLabel)
        addConstraintWithFormat(format: "H:|[v0]|", views: thirdLabel)

    }

}

extension UIView {
    func addConstraintWithFormat(format: String, views: UIView...) -> Void {
        var viewDictionary = [String : UIView]()
        for (index, view) in views.enumerated() {
            let key = "v\(index)"
            viewDictionary[key] = view
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewDictionary))
    }
}

回答1:


So the solution to the problem was to use this method:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    }

This method is used to tell the delegate that the specified cell is about to be displayed in the collection view and at this point, it has been given it's bounds.




回答2:


one more possibility print(cell.frame.size)




回答3:


Maybe this is what you want, try it:

print(cell.photo?.image.size)


来源:https://stackoverflow.com/questions/50896345/how-to-get-the-frame-size-of-a-uiimageview-in-a-uicollectionview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!