Swift 如何通过编程更改使用Interface Builder创建的UIImageView中图像的高度/宽度?
代码如下:Swift 如何通过编程更改使用Interface Builder创建的UIImageView中图像的高度/宽度?,swift,xcode,uiimageview,uiimage,Swift,Xcode,Uiimageview,Uiimage,代码如下: class ViewController: UIViewController { @IBOutlet weak var border: UIImageView! override func viewDidLoad() { super.viewDidLoad() let width = UIScreen.mainScreen().bounds.width let height = UIScreen.mainScreen().bounds.h
class ViewController: UIViewController {
@IBOutlet weak var border: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let width = UIScreen.mainScreen().bounds.width
let height = UIScreen.mainScreen().bounds.height
border.image?.size.width = width
border.image?.size.height = height
}
但我有一个错误说:
无法分配给属性:大小是仅获取的属性
不能以这种方式更改UIImage对象的大小。 但您可以更改UIImageView对象的大小 因此,您的代码应该是:
border.frame?.size.width = width
border.frame?.size.height = height
然后,如果希望UIImageView的内容模式填充比例/纵横比等,请适当设置它
如果要更改UIImage对象的大小,则需要使用CoreGraphics重新绘制它。您不能以这种方式更改UIImage对象的大小。 但您可以更改UIImageView对象的大小 因此,您的代码应该是:
border.frame?.size.width = width
border.frame?.size.height = height
然后,如果希望UIImageView的内容模式填充比例/纵横比等,请适当设置它
如果要更改UIImage对象的大小,则需要使用CoreGraphics重新绘制它。在这种情况下,我建议您更改imageview的大小。更改图像大小是可能的,但在几乎所有情况下更改imageview大小都是可行的。在这种情况下,我建议您更改imageview的大小。更改图像大小是可能的,但在几乎所有情况下,更改imageview大小都是可行的