Suhaib Suhaib - 1 year ago 133
Swift Question

How to programatically change the Width/Height of a UIImageView created via Interface Builder?

I created this UIImageView in Xcode and then added an image. See the picture below :

enter image description here

Then I added this code to try to change the UIImageView but its not working:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var border: UIImageView!

override func viewDidLoad() {

let width = UIScreen.mainScreen().bounds.width
let height = UIScreen.mainScreen().bounds.height

border.frame.size.width = width
border.frame.size.height = height

Answer Source

I believe you have to do this in ViewDidLayoutSubviews, not ViewDidLoad:

override func viewDidLayoutSubviews() {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download