如何在 UICollectionView 单元格中获取 UIImageView 的帧大小 [英] How to get the frame size of a UIImageView in a UICollectionView cell

查看:24
本文介绍了如何在 UICollectionView 单元格中获取 UIImageView 的帧大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 UICollectionViewCellUIImageView 的帧大小.我目前尝试使用 thumbnailImageView.frame.size 来实现这一点,但是无论我从哪里调用这个方法,它都会返回 (0.0, 0.0) .

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.

我想这样做的原因是在图像视图中对图像的边缘进行圆角处理,但是当我使用 .scaleAspectFit 时,我需要知道框架大小和图像大小才能正确圆角角落.

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.

下面是单元格类:

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))
    }
}

推荐答案

所以解决问题的方法就是用这个方法:

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.

这篇关于如何在 UICollectionView 单元格中获取 UIImageView 的帧大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆