如何以编程方式更改通过Interface Builder创建的UIImageView的宽度/高度?

时间:2016-09-07 04:27:42

标签: swift xcode uiimageview

我在Xcode中创建了这个UIImageView,然后添加了一个图像。见下图:

enter image description here

然后我添加了这段代码以尝试更改UIImageView但它无法正常工作:

import UIKit

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.frame.size.width = width
    border.frame.size.height = height
}

1 个答案:

答案 0 :(得分:1)

我相信您必须在 ViewDidLayoutSubviews 中执行此操作,而不是ViewDidLoad:

override func viewDidLayoutSubviews() {



}