我已經在故事板中添加了具有默認圖像的UIImageView。在代碼中,我在運行時更新圖像並調整框架。通過更改框架來調整UIImageView的大小
mainImageView.sd_setImage(with: NSURL(string: urlString) as URL?, completed: {
(image, error, cacheType, url) in
self.setPoints()
})
public func setPoints() {
mainImageView.frame = CGRect(x:x, y:0, width:(mainImageView.image?.size.width)!/1.7, height:(mainImageView.image?.size.height)!/1.7)
mainImageView.contentMode = .scaleAspectFill
mainImageView.setNeedsDisplay()
mainImageView.reloadInputViews()
mainImageView.updateConstraintsIfNeeded()
}
我讀了堆棧溢出的各種帖子,並嘗試使用上述方法調整imageivew的大小,但沒有運氣。有沒有其他方式來調整圖像視圖的大小?
mainImageView通過自動佈局添加?如果是,那麼你應該採取高度和寬度約束和管理它。 – Govaadiyo
mainImageView.translatesAutoresizingMaskIntoConstraints = YES; 你可以把這段代碼放到你的-viewDidLoad方法中或者在mainImageView.sd_setImage之上(使用:... – Baig