我试图通过将imageview内容位置获取为CGRECT来更改NSLayout约束常量。但是我通过contentRect获得的内容位置是不同的。
这是我用来获取内容位置的扩展名。
extension UIImageView {
var contentRect: CGRect {
guard let image = image else { return bounds }
guard contentMode == .scaleAspectFit else { return bounds }
guard image.size.width > 0 && image.size.height > 0 else { return bounds }
let scale: CGFloat
if image.size.width > image.size.height {
scale = bounds.width / image.size.width
} else {
scale = bounds.height / image.size.height
}
let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
let x = (bounds.width - size.width) / 2.0
let y = (bounds.height - size.height) / 2.0
return CGRect(x: x, y: y, width: size.width, height: size.height)
}
override func viewDidLoad(){
super.viewDidLoad()
imageVw.image = imagePassed
imageVw.backgroundColor = colorPassed
setStyle(toTextField: topText)
setStyle(toTextField: bottomText)
let ht = imageVw.contentRect.minY + imageVw.contentSize.height
topConstraint.constant = self.view.frame.height - ht
self.view.layoutIfNeeded()
print("contentRect value \(imageVw.contentRect)")
print("point \(imageVw.contentRect.minY)")
print("image height \(imageVw.contentSize)")
}