如何在 UICollectionView 单元格中获取 UIImageView 的帧大小
How to get the frame size of a UIImageView in a UICollectionView cell
我正在尝试获取 UICollectionViewCell
中 UIImageView
的帧大小。我目前尝试使用 thumbnailImageView.frame.size
来实现此目的,但无论我从何处调用此方法,此 returns (0.0, 0.0)
都是如此。
我想这样做的原因是在图像视图中圆化图像的边缘,但是因为我正在使用 .scaleAspectFit
我需要知道帧大小和图像大小才能正确圆角.
下面是单元格 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))
}
}
也许这就是你想要的,试试吧:
print(cell.photo?.image.size)
还有一种可能打印(cell.frame.size)
所以解决问题的办法就是用这个方法:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
}
此方法用于告诉委托指定的单元格即将显示在集合视图中,此时,它已被赋予边界。
我正在尝试获取 UICollectionViewCell
中 UIImageView
的帧大小。我目前尝试使用 thumbnailImageView.frame.size
来实现此目的,但无论我从何处调用此方法,此 returns (0.0, 0.0)
都是如此。
我想这样做的原因是在图像视图中圆化图像的边缘,但是因为我正在使用 .scaleAspectFit
我需要知道帧大小和图像大小才能正确圆角.
下面是单元格 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))
}
}
也许这就是你想要的,试试吧:
print(cell.photo?.image.size)
还有一种可能打印(cell.frame.size)
所以解决问题的办法就是用这个方法:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
}
此方法用于告诉委托指定的单元格即将显示在集合视图中,此时,它已被赋予边界。