0

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() {
    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 Answer 1

1

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

override func viewDidLayoutSubviews() {



}
Sign up to request clarification or add additional context in comments.

4 Comments

Yup, it worked. Thank you. Just out of curiosity. I tried to use viewWillAppear() to see if I can get the same results but I didn't. Would you know why ?
If not mistaken, while your views from Storyboard exists in ViewDidLoad and ViewDidAppear, the dimensions aren't actually laid out until your viewcontroller calls ViewDidLayoutSubviews. For example, this is why you are able to change a UILabel.text in ViewDidLoad/ViewDidAppear but can't change the UILabel dimensions set in Storyboard until ViewDidLayoutSubviews. Please let me know if this doesn't answer your questions.
Also, in case you were wondering. Storyboard dimensions can be changed in ViewDidAppear(), because it is called after ViewDidLayoutSubviews. The only problem is, you will see the old dimensions which flicks to new dimensions. So the proper place to change Storyboard dimensions is in ViewDidLayoutSubviews.
thank you so much. I'm going to submit another question if you are interested in answering it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.